Inheritance
A feature that allows the derivation of a new class from an existing class. Introduces a way to reuse existing code without duplicating them.
A subclass inherits all the functionality of a superclass, but can also have its own unique attributes and methods.
# Terminologies
Term | Definition |
---|---|
Superclass (also parent or base class) | The class from which subclasses can be created from |
Subclass (also child or derived class) | The class that inherits a superclass |
# Declaration
In many OOP-based languages, a subclass can be created using the :
symbol as such:
- C#
|
|
- Python
|
|
# Inheriting superclass
A subclass can call upon the superclasses’s attributes and properties, though the exact method differs across programming languages.
Some examples when inheriting superclass is used include:
calling the superclass’s constructors to initialise the attributes in the superclass;
calling the superclass’s printing method to retrieve information in the superclass; and
calling a method in the superclass.
C#
|
|