Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. It is one of the fundamental features of object-oriented programming.

Inheritance represents the IS-A relationship, also known as parent-child relationship.

  • Important points

    • In the inheritance the class which is give data members and methods is known as Base class / Super class /Parent class .

    • The class which takes the data members and methods is known as Sub class/ Derived class / Child class .

    • The data members and methods of a class are known as features .

    • The concept of inheritance is also known as re-usability or extendable classes or sub classing or derivation.

  • Why use Inheritance ?

    • To achieve Runtime Polymorphism with the help of Method Overriding .

    • For code Re-usability.

    • It's main uses are to enable polymorphism and to be able to reuse code for different classes by putting it in a common super class

  • Syntax of Inheritance

    • Note: The extends keyword indicates that you are making a new class that is derived from an existing class. The meaning of "extends" is to increase the functionality.


    Syntax:

    class Subclass-Name extends Superclass-Name  
    {  
       //methods and fields  
    }  

  • Real Life Example of Inheritance

    The real life example of inheritance is child and parents, all the properties of father are inherited by his son.

    Real Life Example of Inheritance
  • Java Inheritance Example

     Java Inheritance Example

    Example of Inheritance

    
    //simple exmaple of Inheritance
    class Parent
    {
    	int a=45;
    }
    class Child extends Parent
    {
    	int b=100;
    }
    class Example
    {
    public static void main(String[] args) {
    Child child=new Child();
    System.out.println("Value of Parent a ="+child.a);
    System.out.println("Value of Parent b ="+child.b);
    /*Note we can access parent class
     properties and behaviours using child object */
    }
    }

    Output:
    Value of Parent a =45
    Value of Parent b =100
    
  • Advantage of inheritance

    If we develop any application using concept of Inheritance then that application have following advantages,

    • Application development time is less.

    • Application take less memory.

    • Application performance is enhance (improved).

    • Application execution time is less.

  • Types of inheritance in java

    Based on number of ways inheriting the feature of base class into derived class we have six types of inheritance in java they are:
    1) Single inheritance
    2) Multilevel inheritance
    3) Hierarchical inheritance
    4) Multiple inheritance
    5) Hybrid inheritance
    6) Cyclic Inheritance

    • fig:

      -432572854.jpg
    • fig :

      -1826305520.jpg
    • Note : Cyclic Inheritance is not supported in java.

    • Note : Multiple inheritance is not supported in java through class. but it is supported in java using Interfaces.

  • Single Inheritance Example

    File: Example.java

    Example)
    drag_handle
         class Animal{  
        void eat(){System.out.println("eating...");}  
        }  
        class Dog extends Animal{  
        void bark(){System.out.println("barking...");}  
        }  
        class TestInheritance{  
        public static void main(String args[]){  
        Dog d=new Dog();  
        d.bark();  
        d.eat();  
        }}  
    Output:
    barking...
    eating.
  • Multilevel Inheritance Example

    File: Example.java

    Example)
    drag_handle
         class Animal{  
        void eat(){System.out.println("eating...");}  
        }  
        class Dog extends Animal{  
        void bark(){System.out.println("barking...");}  
        }  
        class BabyDog extends Dog{  
        void weep(){System.out.println("weeping...");}  
        }  
        class TestInheritance2{  
        public static void main(String args[]){  
        BabyDog d=new BabyDog();  
        d.weep();  
        d.bark();  
        d.eat();  
        }}  
    Output:
    weeping...
    barking...
    eating..
  • Hierarchical Inheritance Example

    File: Example.java

    Example)
    drag_handle
         class Animal{  
        void eat(){System.out.println("eating...");}  
        }  
        class Dog extends Animal{  
        void bark(){System.out.println("barking...");}  
        }  
        class Cat extends Animal{  
        void meow(){System.out.println("meowing...");}  
        }  
        class Example{  
        public static void main(String args[]){  
        Cat c=new Cat();  
        c.meow();  
        c.eat();  
        //c.bark();//C.T.Error  
        }}  
    Output:
    meowing...
    eating..
  • Cyclic Inheritance Example

    File : Example.java


    Trying to do the Cyclic Inheritance in java.

    //try to do cyclic inheritance in java
    class A extends B
    {	
    
    }
    class B extends A
    {
    
    }

    Output:
    Example.java:3: error: cyclic inheritance involving A
    class A extends B
    ^
    1 error

    We will get Compile Time Error if we try to do cyclic inheritance.

  • Q) Why multiple inheritance is not supported in java?

    Due to ambiguity problem java does not support multiple inheritance at class level.


    Example

    class A
    {
      void disp()
      {
      System.out.println("Hello");
       }
    }
    class B
    {
      void disp()
      System.out.println("How are you ?");
      }
    }
    class C extends A,B  //suppose if it were
    {
    public static void main(String args[])
    {  
    C obj=new C();  
    obj.disp();//Now which disp() method would be invoked?  
    }  
    }  

    Output:
    error

    In above code we call both class A and class B disp() method then it confusion which class method is call. So due to this ambiguity problem in java do not use multiple inheritance at class level, but it support at interface level.

What next?

The next topic is Association in Java

Share this page