-->

16. Inheritance In Java

Association in Java: Previous                                                                 Next: Aggregation in Java

Inheritance In Java

Inheritance is a way to implement IS-A relationship i.e. parent child relationship. Subclass inherits the subclass properties like data member, methods. Inheritance is the way of re-usability of code. Let us take the example of parent and child. A child inherits the properties of its parent.

Why inheritance is used?

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

 Syntax:

class subclass extends superclass{
   //subclass code
}

Types of inheritance:



1. Single inheritance: 

When a derived class inherits the properties and behavior from a single parent class. It is known as single inheritance.

Example:

/**
 * This program is used for single inheritance example.
 * @author javawithease
 */
class Student {
      String name = "jai";
}
 
public class CollegeStudent extends Student {
      String className = "MCA";     
 
      /**
       * This method is used to show details of a student.
       * @author javawithease
       */
      public void showDetail(){
         System.out.println("Student name = " + name);
         System.out.println("Student class name = " + className);
      }     
 
      public static void main(String args[]){
       //creating subclass object  
       CollegeStudent obj = new CollegeStudent();
 
       //method call
       obj.showDetail();
      }
}

Output:

Student name = jai
Student class name = MCA

2. Multilevel inheritance:

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

Example:

/**
 * This program is used for multilevel inheritance example.
 * @author javawithease
 */
class Student {
      String name = "jai";
}
 
class CollegeStudent extends Student {
      String className = "MCA";
}
 
class McaStudent extends CollegeStudent{
      String semester = "3rd sem.";
 
      /**
       * This method is used to show details of a student.
       * @author javawithease
       */
      public void showDetail(){
         System.out.println("Student name = " + name);
         System.out.println("Student class name = " + className);
         System.out.println("Student semester = " + semester);
      }
}
 
public class StudentTest {
      public static void main(String args[]){
            //creating subclass object
            McaStudent obj = new McaStudent();
 
            //method call
            obj.showDetail();
      }
}

Output:

Student name = jai
Student class name = MCA
Student semester = 3rd sem.

3. Hierarchical inheritance:

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

Example:

/**
 * This program is used for Hierarchical inheritance example.
 * @author javawithease
 */
class Numbers {
      int a = 10;
      int b = 20;
}
 
class AddNumbers extends Numbers {
      /**
       * This method is used to add.
       * @author javawithease
       */
      public void showAdd(){
            System.out.println(a + b);
      }
}
 
class MultiplyNumbers extends Numbers {
      /**
       * This method is used to multiply.
       * @author javawithease
       */
      public void showMultiplication(){
            System.out.println(a * b);
      }
}
 
public class Test {
      public static void main(String args[]){
            //creating base classes objects
            AddNumbers obj1 = new AddNumbers();
            MultiplyNumbers obj2 = new MultiplyNumbers();
 
            //method calls
            obj1.showAdd();
            obj2.showMultiplication();
      }
}

Output:

30
200

Why multiple inheritance is not supported in java?

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

Example:

/**
 * This is used to show that multiple inheritance
 * is not supported in java in case of classes.
 * @author javawithease
 */
class Test1{
      public void show(){
            System.out.println("show details.");
      }
}
 
class Test2{
      public void show(){
            System.out.println("show details.");
      }
}
 
//let multiple inheritance is possible.
public class Test extends Test1, Test2 {
      public static void main(String args[]){
            Test obj = new Test();
            //Ambiguity problem in method call
            //which class show() method will be called.
            obj.show();
      }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
at com.javawithease.business.Test.main(Test.java:19)

Association In Java: Previous                                                                 Next: Aggregation In Java