Advertisement



< Prev
Next >



Types of Inheritance in Java





In Java, inheritance is used when we want to create a class by inheriting the features of another existing class. The class that wants to inherit the feature of another class is called subclass, whereas the class whose features are to be inherited is referred to as superclass. Hence, the feature of inheritance gives Java the power of reusability.

Note : A class inherits another class by using extends keyword.




What features a subclass inherits from its superclass, through inheritance?



Note- Members marked with private access modifiers of a superclass are never inherited.




A simple example of inheritance



class A
{
int a=10;			//default
protected int b=20;		//protected

public void nameA()		//public 
{
System.out.println("A");
}

}


class B extends A // Class B inheriting class A
{
public static void main(String... ar)
{
B ob= new B();
ob.nameA();

System.out.println(ob.a);
System.out.println(ob.b);
}

}


Output is :


A
10
20


Program Analysis


  • In this code, class B has inherited class A by using extends keyword, thereby class B has inherited
    • instance variable, a, of class A, having default access.
    • instance variable, b, of class A, having protected access.
    • method, nameA() of class A, having public access.
    • Hence, reference variable of class B has directly called the instance variable, a, b and nameA(), as post-inheritance these features of class A are also part of class B.





    A subclass can't inherit a private member of its superclass.


    
    class A
    {
    private int a=10;	//private access
    
    private void nameA()	//private access
    {
    System.out.println("A");
    }
    }
    
    
    class B extends A // Class B inheriting class A
    {
    public static void main(String... ar)
    {
    
    B ob= new B();
    ob.nameA();
    
    System.out.println(ob.a);
    
    }
    
    }
    


    Output -:


    
    A.java:18: error: cannot find symbol
    ob.nameA();
      ^
      symbol:   method nameA()
      location: variable ob of type B
    A.java:20: error: a has private access in A
    System.out.println(ob.a);
                         ^
    2 errors
    


    Program Analysis


    • In the class A, instance variable a and method nameA() are marked with private access. Hence, they will not be inherited by class B, and trying to access them using reference of class B gives compile errors.

    • There is no compile error if a class tries to inherit another class, which has private members but the compile error is only thrown when we try to access private members of superclass using reference of subclass(because these private members of superclass were never inherited by its subclass).



    Advertisement




    Types of inheritance :


    • Single level inheritance
    • Multi level inheritance
    • Multiple inheritance





    • Single Level Inheritance

    • When a class extends another class, such type of inheritance is known as single level inheritance.
      class A
      {
      public void showA()
      {
      System.out.println("A");
      }
      }
      
      
      class B extends A		//class B inheriting features of class A
      {
      public void showB()
      {
      System.out.println("B");
      }
      }
      Class B has extended class A and thereby class B has access to the method showA() of class A as its own method.




    • Multilevel Inheritance

    • When a class extends another class, which has itself extended another class, such type of inheritance is known as multi level inheritance.
      class A
      {
      public void showA()
      {
      System.out.println("A");
      }
      }
      
      
      class B extends A 		//class B inheriting features of class A
      {
      public void showB()
      {
      System.out.println("B");
      }
      }
      
      class C extends B		//class C inheriting features of class B
      {
      public void showC()
      {
      System.out.println("C");
      }
      
      public static void main(String... ar)
      {
      C ob = new C();
      ob.showC();
      ob.showB();
      ob.showA();
      }
      }

      Output:

      
      C
      B
      A
      Class A is extended by class B and class B is itself extended by class C, which gives class C a direct access to the method showA() and showB() method of class A and B, by accessing them with an object of class C(without the need to create objects of class A and B).




    • Multiple inheritance

    A java class cannot extend two classes at the same time. But, a class can implement two interfaces at the same time.
    interface Sum 				//interface1
    {
    public void add();
    }
    
    interface Subtract			//interface2
    {
    public void difference();
    }
    
    
    class Math implements Sum,Subtract 	//class Math has implemented multiple interfaces
    {
    public void add()
    {
    System.out.println("Addition");
    }
    
    public void difference()
    {
    System.out.println("Difference");
    }
    }





    Why inheritance is used?


    • Inheritance is used to use the existing features of a class.
    • Inheritance is also used in a situation when we want to create a more specialized version of a generic(general) class, by combining some specialized features with the general features of a general class.

    Let's understand this by an example. We are creating a specialized class, Mango, by adding some specialized methods of Mango and by inheriting some general features of Fruit class.
    //A general class, Fruit
    
    class Fruit
    {
    public void showFruit()
    {
    System.out.println("This is a fruit");
    }
    }
    
    
    //A specialized class, Mango
    
    class Mango extends Fruit		//class Mango inheriting class Fruit
    {
    public void fruitColor()
    {
    System.out.println("Color - Yellow");
    }
    
    public void fruitName()
    {
    System.out.println("Name - Mango");
    }
    
    public void hasSeed()
    {
    System.out.println("Seed - Yes");
    }
    }
    
    
    class A
    {
    public static void main(String... ar)
    {
    Mango ob= new Mango();
    ob.showFruit();
    ob.fruitName();
    ob.fruitColor();
    ob.hasSeed();
    }
    }


    Output -:


    This is a fruit
    Name - Mango
    Color - Yellow
    Seed - Yes


    Program Analysis

    • In this code, we have created a specialized class Mango by inheriting a generic(general) class Fruit by using extends keyword.
    • Hence, class Mango has inherited the general method showFruit() of class Fruit and have also added its own unique specialized methods to display properties of a Mango.





      Inheritance also promotes -:


    • Code reuse.
    • Polymorphism (we will discuss it in the next article).



    Please share this article -




    < Prev
    Next >
    < Encapsulation
    Aggregation and Composition >



    Advertisement

  • Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement