Thursday, April 6, 2017

Inheritance (IS-A)

Inheritance is a compile-time mechanism. A super-class can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance. The superclass and subclass have “is-a” relationship between them.

There are five types of inheritance in Java. They are single, multilevel, multiple, hierarchical and hybrid. I will illustrate them one by one.

Single inheritance: There are only two entities in this inheritance. One is superclass and other is subclass.

Multilevel inheritance: There are more than two entities in this inheritance. Typical example is GFS relationship. That is Grandfather-Father-Son. Son inherits from Father and Father inherits from Grandfather.

Multiple inheritance: A subclass inherits from more than one superclass. But it is not allowed in Java but it can be achieved by implementing interfaces.

Hierarchical inheritance: This kind of inheritance appears in our life so often. A superclass has many subclasses. Dog, cat, tiger, lion are all animals. They inherit from same ancestor.

Hybrid inheritance: Hybrid inheritance is the combination of single and multiple inheritance. By using Interface we can achieve multiple inheritance in Java.

No comments:

Post a Comment

Abstraction vs Interface

Abstract Interface method abstract and concrete abstract only, support default and static method ...