Java Inheritance

Inheritance refers to a way of implementing an IS-A relationship i.e. parent-child relationship. A subclass inherits the superclass properties like data members and methods. Inheritance is a way of code re-usability. Let us consider an example of parent and child. A child inherits the properties of its parent.

Use of Inheritance

  1. Code re-usability.
  2. Run-time polymorphism.

Syntax

class SubClass extends SuperClass{
   //Subclass code block
}

Types of inheritance

1. Single inheritance:

It represents a case when a derived class inherits the properties and behavior from a single-parent class. It is known as single inheritance.

Example:

package com.w3schools;

class Student {
      String studentName = "Jon";
}

public class CollegeStudent extends Student {
      String courseName = "Btech";     

      public void showDetail(){
         System.out.println("Student Name = " + studentName);
         System.out.println("Student Course Name = " + courseName);
      }     

      public static void main(String args[]){
       //Create subclass object  
       CollegeStudent obj = new CollegeStudent();

       //Method call
       obj.showDetail();
      }
}

Output:

Student Name = Jon
Student Course Name = Btech

 

2. Multilevel inheritance:

It represents a case when a derived class inherits the properties and behavior from a derived class. It is known as multilevel inheritance.

Example:

package com.w3schools;

class Student {
      String studentName = "Jon";
}

class CollegeStudent extends Student {
    String courseName = "Btech";     
}

public class BTechCollegeStudent extends CollegeStudent {
      String currentSem = "4th Sem";     

      public void showDetail(){
         System.out.println("Student Name = " + studentName);
         System.out.println("Student Course Name = " + courseName);
         System.out.println("Student Current Sem = " + currentSem);
      }     

      public static void main(String args[]){
       //Create subclass object  
       BTechCollegeStudent obj = new BTechCollegeStudent();

       //Method call
       obj.showDetail();
      }
}

Output:

Student Name = Jon
Student Course Name = Btech
Student Current Sem = 4th Sem

3. Hierarchical inheritance:

When two or more derived class inherits the properties and behavior from the same parent class. It is known as hierarchical inheritance.

Example:

package com.w3schools;

class Numbers {
      int num1 = 100;
      int num2 = 200;
}

class AddNumbers extends Numbers {
      public void showAdd(){
    	  System.out.println("Addition of Numbers: ");
          System.out.println(num1 + num2);
      }
}

class MultiplyNumbers extends Numbers {
      public void showMultiplication(){
    	  System.out.println("Multiplication of Numbers: ");
          System.out.println(num1 * num2);
      }
}

public class HierarchicalInheritanceTest {
      public static void main(String args[]){
            //Create base classes objects
            AddNumbers obj1 = new AddNumbers();
            MultiplyNumbers obj2 = new MultiplyNumbers();

            //Method calls
            obj1.showAdd();
            obj2.showMultiplication();
      }
}

Output:

Addition of Numbers: 
300
Multiplication of Numbers: 
20000

Why multiple inheritance is not supported in Java?

Multiple inheritance is not supported by Java because of the ambiguity problem. Let us consider the below example. We have two classes Test1 and Test2 which have the same method show(). If multiple inheritance is possible then the Test class can inherit properties and behaviour of both Test1 and Test2 classes. Now Test class has two show() methods inherited from Test1 and Test2. The problem occurs now in a method call when the show() method is called with the Test class object which method will be called, the Test1 class or the Test2 class? This is the reason why multiple inheritance is not supported in Java.

Example:

package com.w3schools;

class Test1{
      public void show(){
            System.out.println("Show Test 1 details.");
      }
}

class Test2{
      public void show(){
            System.out.println("Show Test 2 details.");
      }
}

//let multiple inheritance is possible.
public class MultipleInheritanceTest extends Test1, Test2 {
      public static void main(String args[]){
    	  MultipleInheritanceTest obj = new MultipleInheritanceTest();
            //Ambiguity problem in method call 
            //which class's show() method will be called.
            obj.show();
      }
}

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

    at com.w3schools.MultipleInheritanceTest.main(MultipleInheritanceTest.java:17)

 

Please follow and like us:
Content Protection by DMCA.com