Inheritance is one of the important features of OOPS concepts. It helps in the reuse of code by inheriting the features of one class known as parent class by another class known as its child class. When inheriting extends to more than 2 levels, it is known as multilevel inheritance. When one new class derives features from a class that has been derived from one base class, it is said to be a multilevel inheritance. The new class is said to be a grandchild of a parent class.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsFor e.g., A is the parent, class B is its child, And C is the child class of B and grandchild of A. Similarly, A is parent class for class B and grandparent for class C.
Let us see the syntax of multilevel inheritance in java which is given below:
class A{ //class A is parent of class B //class A is grand parent of class C public A(){ //A constructor } public void fun1(){ //function in Parent Class } } class B extends A{ //class B is a child class of class A //class B is a parent class of class C public B(){ //class B constructor } } class C extends B{ //class C is a child class of class B //class C is grand child class of class A public C(){ //Class C constructor } } public class Test{ public static void main(String[] args){ C obj = new C(); } }
For the implementation of multilevel inheritance, there must be one base class, e.g., A. Then there must be a derived class B which extends class A, and class C extends A.
Multilevel inheritance is a type of extending the features of one derived class to another new class. Since the parent class features are extended up to multiple levels thus this type of inheritance is known as multilevel inheritance. When a child class extends a parent class, it can use all the features parent class. Thus if there is a class that extends features of this derived class, then it is said to be a grandchild of the base class that has all features of parent and child class.
Consider a class A as a parent class, class B as a child class of class A and class C as a child class of class B. and when an object is created for class C, say obj as given above. When we declare this object, the constructor of class C is called. As we know, while constructor of a child class first constructor of its parent class is called. Thus when we call C() – then B() constructor gets called, and further, as B is a child class for class B, thus A() is called. Then control gets back to the child class. Thus in the following series, constructors are executed:
A() – > B() – > C()But when a method is called using this object of a child class, first it is checked if that method with the same signature is present in the child class. If not, then control is directed to its parent class to find the method. Thus, in this case, when fun1() is called using object obj, control goes to C and find there is no such method; thus, control goes to B and thus class A.
The reason for this is because one can easily redefine the methods of the parent class in its child class, known as method overriding. Thus first preference is given to the overridden method. In this way, Multilevel inheritance implements the inheritance feature in classes at multiple levels. This type of inheritance is most often used while implementing data augmentation – that is, the process of increasing the diversity and amount of existing data without updating the existing code. It also helps to introduce variability to one’s available training model by applying simple transformations.
Let us see some of the examples of multilevel inheritance in java.
Code:
class Electronics { public Electronics(){ System.out.println("Class Electronics"); } public void deviceType() { System.out.println("Device Type: Electronics"); } } class Television extends Electronics { public Television() { System.out.println("Class Television"); } public void category() { System.out.println("Category - Television"); } } class LED extends Television { public LED() { System.out.println("Class LED"); } public void display_tech() { System.out.println("Display Technology- LED"); } } public class Tester { public static void main(String[] arguments) { LED led = new LED(); led.deviceType(); led.category(); led.display_tech(); } }
Output:
Explanation: In the above example, the class Electronics is a general class that provides a method device_type() for all electronic devices. Then we have class television that extends the Electronics class, which specifies the Electronics device and has a method name – category() to display the type of electronic device. Then class LED extends the Television class to specify the technology used for its display. It has the method display_tech() to show the technology is LED.
In the main method, when we make an object of the LED class, we use it to call the method for all the parent class. When to call a constructor of a child class, the parent class’s constructor is called first; thus, when new LED() is called first new Television() is called. Further in this constructor new Electronics() gets called and displays – Class Electronics.
Then it is returned to the Television constructor and displays Class Television and then returned to LED class and displays- Class LED. When a method is called using an object of the LED class, first control goes to LED class and tried to find method – device_type() and went to Television class to find the method on not finding it in LED class and further when not found it in Television class also goes to its further superclass Electronics and finds that method and execute it.
Multilevel inheritance is a great technique to implement inheritance’s main advantage, i.e. code reusability and readability through multiple levels. It helps to introduce variability and diversity to the existing code, which provides the basic training material. This procedure is known as code documentation.
The above is the detailed content of Multilevel Inheritance in Java. For more information, please follow other related articles on the PHP Chinese website!