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.
| Feature | Compile-Time (Overloading) | Runtime (Overriding) |
|---|---|---|
| Resolution time | At compilation | At runtime |
| Keyword used | Same method name, different params | @Override annotation (Java) |
| Binding type | Static binding | Dynamic binding |
| Example | add(int,int) vs add(double,double) | Animal.sound() overridden in Dog |
| Performance | Faster (resolved early) | Slightly slower (resolved late) |
Wikimedia Commons, CC BY-SA
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.
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.
An interface in OOP is a fully abstract type that defines a set of method signatures (a contract) that any implementing class must fulfil, without specifying how those methods work. Interfaces enable multiple inheritance of type in languages like Java and C#, allowing a class to implement several interfaces simultaneously. They promote loose coupling in software design by allowing different classes to be used interchangeably when they implement the same interface.
From Greek "polys" (many) and "morphe" (form), meaning "many forms". In biology, polymorphism refers to multiple forms of a species; the term was applied to programming by Strachey (1967) and formalised by Cardelli and Wegner in their 1985 paper on types.