INHeritance
INHeritance
OOP
What is Inheritance?
The capability of a class to derive properties and
characteristics from another class is called Inheritance
It allows us to create a new class (derived class) from an
existing class (base class)
The new class created is called “derived class” or “child
class” and the existing class is known as the “base class” or
“parent class”
Sub Class: The class that inherits properties from another
class is called Subclass or Derived Class.
Super Class: The class whose properties are inherited by a
subclass is called Base Class or Superclass.
What is Inheritance?
The derived class inherits all
the properties of the base
class, without changing the
properties of base class
It may add new features to its
own
These new features in the
derived class will not affect
the base class
Why and when to use inheritance?
Why and when to use inheritance?
is-a relationship
class. Then both public members and protected members of the base
class will become protected in the derived class.
Private Mode: If we derive a subclass from a Private base class.
Then both public members and protected members of the base class
will become Private in the derived class.
Note: The private members in the base class cannot be directly
accessed in the derived class, while protected members can be directly
accessed.
Modes of Inheritance
Modes of Inheritance
Example
Example:
1. class ABC : private XYZ //private derivation
{ }
2. class ABC : public XYZ //public derivation
{ }
3. class ABC : protected XYZ //protected
derivation
{ }
4. class ABC: XYZ //private derivation
by default
{ }