![]() |
![]() |
Java Interface | |
Java Class vs Interface | |
Java Multiple Inheritance | |
Java Nested Interface |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
Inheritance is the method of deriving a new class from existing one.The main idea behind the using inheritance is the re usability of code instead of writting again and again.
The existing or old class which is reused called as base class or parent class or superclass. The new class which is going to use that class called as Derived class or child class.
The new class can have its own properties and well as parent properties.
Note : Java doesn't support the Multiple and Hybrid Inheritance. Which can achieve this by using interface concept, we see later on. |
In Single Inheritance, There are two classes in which one base class and another is derived class. The derived class accesses all the properties of base class based on the visibility specifier.
In fig. Class A is the base class and Class B is the derived class. The arrow always points towards the Derived classes.
extends keyword is used for accessing the properties of class.
In Multiple Inheritance, There are multiple base classes which are accesses by only single derived class.
In fig. Class A and Class B are the two base classes which are derived into derived class C
While accessing the properties of base classes all the properties of base classes are copied into base class based on their their visibility
In Hierarchical inheritance, There is only one base class but multiple Derived Classes
In fig. Class A is only one base class and Class B,C,D are the three derived classes.
Multiple base classes accesses the properties of single base class. But the base decides which kind of data should be given for others and which not.
In Multilevel inheritance There is a base class which is extended into derived class. Then again derived class acts as a base class for next derived class.
In fig. Class A is base class which is accessed by Derived class B, Again class B is accessed by Class C. But class B acts as base class for class C.
Hybrid Inheritance is a combination of Single and Multiple Inheritance or we can say Hybrid Inheritance is a Combination of more than one type of Inheritance.
There could be situations where we need to apply two or more types of inheritance to achieve some need in the program.That moment hybrid inheritance is very much important
In fig. Class A,B,C are together called as Multilevel Inheritance and Class D,E are called as SIngle Inheritance.