Multilevel Inheritance in Java With Example Programs

What is Multilevel Inheritance In Java?

  • Remove An Element From Collection Using Iterator Object In Java
  • Java Thread By Extending Thread Class – Java Tutorials

In Java (and in other object-oriented languages) a class can get features from another class. This mechanism is known as inheritance.

  • When multiple classes are involved and their parent-child relation is formed in a chained way then such formation is known as multi-level inheritance.
  • In multilevel inheritance, a parent a class has a maximum of one direct child class only.
  • In multi-level inheritance, the inheritance linkage is formed in a linear way and minimum 3 classes are involved.

Code re-usability can be extended with multi-level inheritance.

multilevel Inheritance In Java

Example:

Suppose, we have a form as shown above (class A is the parent of class B and class B is the parent of class C), then features of A are available for B, and features of B (including that of A) are available for C. So, class C get features of both A and B.

In this case, class B is the parent to C and child to A. such classes are generally known as intermediate classes. When an object of class C is created, constructors of all the three classes will be executed.

Even though the control goes to the constructor of C first, the actual sequence of execution will be the constructor of A first, the constructor of B next and constructor of C at last.

The following example gives a bit more clarity on multi-level inheritance.

Example:

Output:

In the above example, as discussed above, the constructor of grandparent class (Person) is executed first, then parent class (Emp) and then that of child class (Manager).

Then the method nationality() of Person and the method organization() of Emp are accessed through Manager object as if they are own methods of Manager. Then the method subordinates() of Manager (its own) is called.

We can see the corresponding outputs here. At last, we have invoked the method place() which exists in all the three classes. Here, the method of Person is overridden in Emp and the same method is overridden again in Manager.

So the version in Manager got executed. Had that method been not overridden in Manager, then the invocation could have executed the Emp version.

  • If we don't want a parent class feature (method or variable) to be available outside the class then we can make it private.
  • In that case, such feature will not be available to child class also.
  • If we want to control the accessibility so that they a feature can be accessed in the current package but not from outside then we make the feature as default (normal).

In the above example, all the methods are normal (default). So they cannot be accessed from other packages. If we make a feature a protected, then they can be accessed anywhere in the current package and from child classes of other packages.

If we make a feature as public, then there is no restriction, such elements can be accessed from anywhere.

Example:

Output:

Example:

Output:

Through interfaces also, we can see multi-level inheritance.

Example:

A similar thing can be achieved using abstract classes also.

Example:

Even though we can have any number of classes (abstract classes) involved in this sequence, a final class cannot be used as a parent class. So the following code is not valid.

Example:

Other formations of inheritance:

Single-level inheritance

When there are only 2 classes involved and one is the parent and another is the child, such inheritance is known as a simple inheritance (single-level inheritance).

Example:

Hierarchical inheritance

When there are 3 or more classes involved and there is a common parent for 2 or more child classes, such relation is hierarchical inheritance.

Example

Multiple-inheritance

When there are multiple parent classes for a child class, such inheritance is known as multiple inheritances. This kind of inheritance is not supported in Java with classes. When interfaces are involved we can see multiple-inheritance in Java.

Example:

lewisroich1946.blogspot.com

Source: https://javatutoring.com/multilevel-inheritance-in-java/

0 Response to "Multilevel Inheritance in Java With Example Programs"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel