INHERITANCE
Ø Process of deriving
properties or method from one class to another class.
Ø Implements IS-A relationship.
Ø Achieves Code Reusability.
Ø Super class (Parent class) - the class from which the properties are derived.
Ø Sub class (Child class) - the class where the properties are derived to.
Ø Super class is also
called Base class.
Ø Sub class is also called
Derived class.
REAL
TIME EXAMPLE:
FATHER-SON relationship.
v Superclass
- Father.
v Subclass
- Son.
Method or Properties - Father's asset, Father's characteristics.
Types
of Inheritance:
Ø Single Inheritance
Ø Multiple Inheritance
Ø Multilevel Inheritance
Ø Hierarchical Inheritance
Ø Hybrid Inheritance
SINGLE INHERITANCE:
üAs the name indicates, it contains Single parent class and single child class.
üWhen the properties or behaviour derived from single base class to single child class is called Single Inheritance.
REAL TIME EXAMPLE:
üContains Multiple parent class and single child class.
üWhen the properties and behaviour are derived to single child class from more than one parent class is called Multiple Inheritance.
REAL TIME EXAMPLE:
MULTILEVEL
INHERITANCE:
üWhen the properties are derived to child class from another Derived class is called Multilevel Inheritance.
üHere the child class implicitly inherit the properties of base class along with the properties of base class’s parent class(base class).
Here Class A is the base
class of Class B and also implicit base class of Class C.
Class B is the base
class of Class C and also Sub class of Class A.
Class C is the child
class of Class B.
Class C inherits the
properties of both Class A and Class B.
REAL TIME EXAMPLE:
Here,
üAnimals is the Base class for both Mammals and Dogs Classes.
üMammal is the sub class of Animal but Base Class of Dogs.
üDogs is the Sub Class and derives properties of both Mammals and Animals Base classes.
HIERARCHICAL
INHERITANCE:
üContains Single Parent
class (Base Class) and Multiple Child Class (Sub Class).
üWhen more than one sub classes derive properties or behaviour from the same base class is called Hierarchical Inheritance.
REAL TIME EXAMPLE:
Here,
üAnimal is the Base Class.
üDogs and Elephants are the Sub Classes of Animal.
HYBRID INHERITANCE:
üCombination of more than
one inheritance.
ØClass A to Class D-Single
Inheritance.
ØClass A to Class B and Class B to
Class D-Multilevel Inheritance.
ØClass A to Class B and
Class A to Class C-Hierarchical Inheritance.
ØClass B and Class C to Class D- Multiple Inheritance.
REAL TIME EXAMPLE:
Here,
üClass Animals to Class Elephants-Single Inheritance.
üClass Animals to Class Mammals and Class Mammals to Class Elephants-Multilevel Inheritance.
üClass Animals to Class Mammals and Class Animals to Class Herbivores-Hierarchical Inheritance.
üClass Mammals and Class Herbivores to Class Elephants- Multiple Inheritance.
0 Comments