Class
- In Turkish
- Sınıf
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
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
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- Data TypeProgramming Fundamentals, p. 14A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
- Design PatternSoftware Architecture, p. 13A design pattern is a proven, reusable solution to a common problem in software design, described as a general template rather than as finished code.
- Singleton PatternSoftware Architecture, p. 41The singleton pattern is a creational design pattern that ensures a class has only one instance and provides a single, global point of access to it.
- TypeScriptWeb Development, p. 50TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
- ObjectProgramming Fundamentals, p. 39An object is a bundle of related data and behavior: values in named fields, often called properties, and functions that work on them, called methods.
- MethodProgramming Fundamentals, p. 37A method is a function that belongs to an object or class and is called on it, so it can read and change that object's data, as in user.rename("Ada").
- ConstructorProgramming Fundamentals, p. 13A constructor is a special method that runs when a new object is created from a class, setting up its starting values so it is ready to use.
Spotted a mistake or something missing on this page?Suggest an edit