Class (Programming)
An extensible program-code template for creating objects, providing initial values for state (variables) and implementations of behavior (methods).
A class acts as a custom data type. For example, a Car class might define variables for color and speed, and methods for accelerate() and brake(). If you create a myCar object from that class, you can set its color to red without affecting the yourCar object.
Classes support the three main pillars of OOP:
1. Encapsulation: Hiding the internal state and requiring all interaction to be performed through an object's methods (using access modifiers like private and public).
2. Inheritance: Creating a new class (subclass/child) that inherits attributes and methods from an existing class (superclass/parent).
3. Polymorphism: The ability of different classes to be treated as instances of the same class through a common interface.
graph LR
Center["Class (Programming)"]:::main
Pre_object_oriented_programming["object-oriented-programming"]:::pre --> Center
click Pre_object_oriented_programming "/terms/object-oriented-programming"
Rel_object["object"]:::related -.-> Center
click Rel_object "/terms/object"
classDef main fill:#7c3aed,stroke:#8b5cf6,stroke-width:2px,color:white,font-weight:bold,rx:5,ry:5;
classDef pre fill:#0f172a,stroke:#3b82f6,color:#94a3b8,rx:5,ry:5;
classDef child fill:#0f172a,stroke:#10b981,color:#94a3b8,rx:5,ry:5;
classDef related fill:#0f172a,stroke:#8b5cf6,stroke-dasharray: 5 5,color:#94a3b8,rx:5,ry:5;
linkStyle default stroke:#4b5563,stroke-width:2px;
🧠 Knowledge Check
🧒 Explain Like I'm 5
Think of a class as a cookie cutter, and an [object](/en/terms/object) as the actual cookie. The cookie cutter determines the shape (what data and abilities the cookie has). You can use the same cookie cutter to make hundreds of cookies. They all have the same shape, but one might have chocolate chips and another might have sprinkles (their unique state).
🤓 Expert Deep Dive
Memory Layout and Instantiation
When a class is instantiated (using a constructor), the runtime allocates memory on the heap (for the object's instance variables) and returns a reference. Methods are typically not duplicated per object; instead, a single copy resides in memory, and the compiler implicitly passes a reference to the current object (this or self) to the method.
Dynamic Dispatch (vtables)
In languages like C++ and Java, polymorphism is implemented via virtual method tables (vtables). A class containing virtual functions has a hidden pointer (vptr) added to its memory layout, which points to an array of function pointers. At runtime, the program looks up the correct method implementation in the vtable based on the actual object type, enabling dynamic dispatch.
Paradigm Shifts
While class-based OOP (originating with Simula and Smalltalk) dominated the 1990s and 2000s (Java, C++, C#), modern paradigms often favor composition over inheritance, utilizing interfaces, traits (Rust), or prototype-based inheritance (JavaScript).
❓ Frequently Asked Questions
What is the difference between a class and an object?
A class is the blueprint or definition (the idea). An object is the actual instance created from that blueprint (the physical thing in memory). For example, 'House' is a class, but the specific house you live in is the object.
What is a constructor in a class?
A constructor is a special method inside a class that is automatically called when a new object is created. It is used to set the initial values (state) of the object.
Do all programming languages use classes?
No. While languages like Java, C++, and Python rely heavily on classes (Object-Oriented Programming), other languages use different paradigms. For example, C uses procedural programming, Haskell uses functional programming, and JavaScript originally used prototype-based inheritance.