Skip to main content

Class

In Turkish
Sınıf
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/class

In short

A class is a blueprint in object-oriented programming that defines the data and behavior shared by a group of objects, which are created from it as instances.

What is a class in programming?

A class describes what a certain kind of object looks like and what it can do. It lists the properties, which are the data each object stores, and the methods, which are the functions each object can run. You then create as many objects, called instances, from the class as you need, each with its own values.

When you create an instance, usually with the new keyword, a special method called the constructor runs to set up the object's starting values. Inside the class's methods, a keyword such as this in JavaScript or self in Python refers to the specific instance being used. Classes can also inherit from other classes, for example with extends, reusing the parent's code while adding or overriding behavior.

A cookie cutter is a classic analogy for a class. The cutter defines the shape, and every cookie you press out is a separate instance with that shape, even if you decorate each one differently. Classes are central to languages such as Java, C#, C++, Python, and Swift, where they model things like users, orders, and user interface components.

A class is often confused with an object. The class is the definition, while an object is a concrete instance that exists in memory while the program runs. In JavaScript, classes were added in ES2015 as a cleaner syntax over the language's older prototype-based system, so they behave slightly differently from classes in Java or C#.

Key takeaways

  • A class defines the properties and methods that its objects share.
  • An object created from a class is called an instance.
  • The constructor sets up each new instance's starting state.
  • Classes can inherit from other classes to reuse and extend behavior.

Example

A class and a subclass in JavaScriptjavascript
class Animal {
  constructor(name) {
    this.name = name; // each instance stores its own name
  }
  speak() { return `${this.name} makes a sound`; }
}

// Dog inherits from Animal and overrides speak()
class Dog extends Animal {
  speak() { return `${this.name} barks`; }
}

const rex = new Dog("Rex"); // create an instance
rex.speak(); // "Rex barks"

Readers ask

What is the difference between a class and an object?

A class is the template that defines properties and methods. An object is a specific instance built from that class, with its own data, so one User class can produce thousands of different user objects.

What is a constructor?

A constructor is a special method that runs automatically when you create a new instance of a class. It usually sets the object's initial property values from the arguments passed in, as in new User("Ada").

Does JavaScript have classes?

Yes. JavaScript has had class syntax since ES2015, but under the hood it still uses prototypes, which are objects that other objects inherit from. The syntax simply makes object-oriented code easier to read and write.

Often compared

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings