Inheritance
Inheritance
in programming
Vehicle
Car Motorcycle
Chapter 13
DEFINITION
What is meant by
‘Inheritance’?
Inheritance makes it possible to create a new
child class that inherits the attributes and
methods of the original (parent) class.
TERMINOLOGY
The term ‘parent class’ is also referred to as
superclass or base class, while ‘child classes’ can
also be referred to as subclasses or derived
classes.
Purpose
Inheritance is a technique that helps reduce
repeated code. Code that is inherited from a
parent class does not need to be redefined. This
means that if the implementation detail needs
to change, it only needs to be changed in one
place (and all child classes will access the new
implementation).
Animal
Types of C
Inheritance MULTILEVEL
Multilevel inheritance is the process of
deriving a class from another (already)
derived class. When one class inherits
another class it is further inherited by
another class.
A Parent Class
C Child Class
Real-Life Analogy
A parent class is similar to a parent in Genetics, since this class contains fundamental traits (attributes and
methods) that can be passed down.
A child class is like an offspring, this class inherits the traits of the parent but can also introduce new
characteristics or override existing ones, much like how a child may express a variation of inherited genetic
traits.
The reuse of code that comes with inheritance is similar to Evolution in Biology, where beneficial traits are
inherited and passed down, allowing both organisms and programs to remain efficient and adaptable over
time.