A class in object-oriented programming is a blueprint or template that defines the attributes (data fields) and methods (functions) common to all objects of that type. When a class is instantiated, it produces an object — a concrete instance that holds its own copy of the class's attributes. Classes enable code organisation by grouping related data and behaviour together, forming the foundational building blocks of OOP design.
| Aspect | Class | Object |
|---|---|---|
| Definition | Blueprint / template | Instance of a class |
| Memory | No memory allocated at definition | Memory allocated on creation |
| Creation | Declared with `class` keyword | Created with `new` keyword (or constructor call) |
| Count | Defined once | Can create many objects from one class |
| Example | Car (blueprint) | myCar = new Car() |
Wikimedia Commons, CC BY-SA
Object-Oriented Programming (OOP) is a programming paradigm that organises software design around data, called objects, rather than functions and logic. Each object encapsulates data (attributes) and behaviour (methods), and objects interact with one another to build complex systems. The four core principles of OOP — encapsulation, inheritance, polymorphism, and abstraction — promote code reusability, modularity, and maintainability.
Inheritance is an OOP mechanism whereby a child class (subclass) automatically acquires the attributes and methods of a parent class (superclass), enabling hierarchical relationships between classes. The subclass can extend or override inherited behaviour without modifying the original parent class, promoting code reuse and reducing duplication. Inheritance models real-world "is-a" relationships, such as a Dog being an Animal.
Encapsulation is the OOP principle of bundling an object's data (attributes) and the methods that operate on that data into a single unit (a class), while restricting direct external access to the internal state. By declaring attributes as private and exposing them only through public getter and setter methods, encapsulation enforces data integrity and hides implementation details. This protects objects from unintended interference and simplifies maintenance since internal changes do not affect external code.
The word "class" derives from Latin "classis" meaning a group or division. In programming, it was introduced by Ole-Johan Dahl and Kristen Nygaard in Simula 67 (1967), the first language to feature classes, later popularised by Smalltalk and C++.