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.
| Pillar | Description | Key Benefit | Example |
|---|---|---|---|
| Encapsulation | Bundling data and methods in a class | Data hiding and security | Private bank account balance |
| Inheritance | A class derives properties from a parent class | Code reuse | Dog extends Animal |
| Polymorphism | Same method name, different behaviours | Flexibility | draw() for Circle and Square |
| Abstraction | Hiding implementation details | Simplicity | Using a car without knowing the engine |
Wikimedia Commons, CC BY-SA
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.
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 term "object-oriented" was coined by Alan Kay in the early 1960s while developing Simula and later Smalltalk at Xerox PARC. "Object" derives from Latin "objectum" (thing thrown before the mind); the paradigm was popularised through C++ (1983) and Java (1995).