What is multilevel inheritance in JAVA

What is multilevel inheritance in JAVA

Multi Level Inheritance in Java

Java is an object-oriented language. One of the core concepts in the language is – Inheritance i.e. a child object may acquire all the properties and behavior of the parent object. It is an essential component of OOPS concepts and supports ‘Reusability’ in Java. Now, the question comes what is the multilevel inheritance in Java and how does it help you in programming? In this blog, we’ll try to cover this with an example along with few more questions a novice developer might have –

“What is multilevel inheritance?”

“What is the difference between multiple and multilevel inheritance?”

“Can you demonstrate multilevel inheritance in Java using a small program?”

Multilevel Inheritance in Java

Multilevel inheritance occurs when we have a structure where a class is extending a class, and that class is also extending another class. For example, look at the image below. Here child class is extending a class called L1, and class L1 extends another class L2. This is known as multilevel inheritance. If you look carefully, you’ll realize that class child implicitly inherits all the properties and behaviors of class L2.

Let’s understand this with a real-life example.

Multilevel inheritance in Java – child class inherits all the properties of parent class L1 and grandparent class L2

Person.java

public class Person {
 private String name;
 
 public Person (String name){
  this.name = name;
 }
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public void display() {
  System.out.println("Person is  - " + name);
 }
}

Teacher.java

public class Teacher extends Person{
 private String course;
 
 public Teacher(String name, String course) {
  super(name);
  this.course = course;
 }
 public String getCourse() {
  return course;
 }
 public void setCourse(String course) {
  this.course = course;
 }
 
 public void display() {
  super.display();
  System.out.println("Course name is - " + course);
 }
}

TeachingAssociate.java

public class TeachingAssociate extends Teacher{
 private int weeklyHrs = 0; 
 
 public TeachingAssociate(String name, String course, int weeklyHrs) {
  super(name, course);
  this.setWeeklyHrs(weeklyHrs);
 }
 
 public int getWeeklyHrs() {
  return weeklyHrs;
 }
 
 public void setWeeklyHrs(int weeklyHrs) {
  this.weeklyHrs = weeklyHrs;
 }
 public void display() {
  super.display();
  System.out.println("No. of Hours per week - " + weeklyHrs);
 }
}

MultilevelInheritance.java

public class MultilevelInheritance {
 public static void main(String[] args) {
  TeachingAssociate ta = new TeachingAssociate("Chad", "Mathematics", 13);
  ta.display();
 }
}

Output –

Person is – Chad   Course name is – Mathematics No. of Hours per week – 13

Hope this example helps you understand this aspect of inheritance in Java. Coming back to the questions noted at the start of the blog –

Does Java support multilevel inheritance?

Yes, Java does support this. Note that Java does NOT support multiple inheritance.

What is multilevel inheritance?

As demonstrated above, a class extends another parent class and the parent class extends a’grand-parent’ class.

What is the difference between multiple and multilevel inheritance?

Can you demonstrate multilevel inheritance in Java using a small program?

Please feel free to ask your questions in the comments section and let us know if you feel the article has helped you in understanding this inheritance concept better.

Reference about simple inheritance in Java – https://www.w3schools.com/java/java_inheritance.asp

Also Read – What is default method in interface?

Leave a Reply

Your email address will not be published. Required fields are marked *