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.
| Type | Description | Example | Supported in Java? |
|---|---|---|---|
| Single | One subclass inherits one superclass | Dog extends Animal | Yes |
| Multilevel | Chain of inheritance | Poodle extends Dog extends Animal | Yes |
| Hierarchical | Multiple subclasses, one superclass | Cat, Dog both extend Animal | Yes |
| Multiple | One subclass, multiple superclasses | FlyingFish extends Fish, Bird | No (via interfaces) |
| Hybrid | Combination of above types | Complex class hierarchy | No (via interfaces) |
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.
Polymorphism is the OOP principle that allows a single interface or method name to represent different underlying implementations depending on the object type. There are two main forms: compile-time polymorphism (method overloading, where multiple methods share a name but differ in parameters) and runtime polymorphism (method overriding, where a subclass provides its own implementation of a parent's method). Polymorphism makes programs more flexible and extensible, allowing new object types to be added without changing existing code.
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.
From Latin "inheritare" meaning to receive as an heir. In programming, the concept was formalised in Simula 67 by Dahl and Nygaard, where subclasses could extend superclasses. The term became widespread with C++ (1983) and Java (1995).