Image

Java - Core Java - Inheritance

Inheritance

To inherit a class, you simply incorporate the definition of one class into another by using the extends keyword. To see how, let’s begin with a short example. The following program creates a super class called A and a subclass called B. Notice how the keyword extends is used to create a subclass of A.

Inheritance is the process of taking the features (data members + methods) from one class to another class. The class which is giving the features is known as base/parent class.

The class which is taking the features is known as derived/child/sub class Instance is known as sub classing or derivation or extendable classes or reusability


Types of inheritance

  • Single level inheritance
  • Multi level inheritance
  • Hierarchical inheritance
  • Single level inheritance

    Single class is one in which there exist one base or one derived class. Single inheritance is easy to understand. When a class extends another one class only when we call a single inheritance .the below diagram show that derived class extends only one class which is base class. here the base class is parent class and derived class is child class of base class.

    1567100212-image.png
    class A {
       String as = name();
       String name() {
         return "A";
       }
     }
     
     class B extends A {
       String as = super.as;
       String name() {
        return "B";
       }
     }
     public class HFC {
       public static void main(String[] args) {
         A a = new B();
         System.out.println(a.as);
       }
     }
    O/P
    D:\classes>javac HFC.java
    D:\classes>java HFC
    B

    Multilevel inheritance

    Multilevel inheritance is one which there exist single base class single derived class and number of intermediate class base class is known as multilevel inheritance.

    1567100223-image.png Example
    class HFC4
    {
      public static void main(String[] args) 
      {
        PolyA ref1=new PolyC();
        PolyB ref2=(PolyB)ref1;
        System.out.println(ref2.g());
      }
    }
    class PolyA
    {
      private int f()
      {
        return 0;
      }
      public int g()
      {
        return 3;
      }
    };
    class PolyB extends PolyA
    {
      private int f()
      {
        return 1;
      }
      public int g()
      {
        return f();
      }
    };
    class PolyC extends PolyB
    {
      public int f()
      {
        return 2;
      }
    };
    O/P
    D:\classes>javac HFC4.java
    D:\classes>java HFC4
    1

    Hierarchical inheritance

    Hierarchical inheritance is one in which there exist one base class and number of derived classes. in such kind of inheritance one class is inherited by many sub classes.

    1567100234-image.png Program to demonstrate hierarchical inheritance
    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("meow...");
      }
    }
    
    class HierarchicalInheritance{
      public static void main(String args[]){
      Cat c=new Cat();
      c.meow();
      c.eat();
      //c.bark();// Error
      }
    }  
    Output:
    meow...
    eating...
    Why multiple inheritance not support in java?

    There is a ambiguity problem occur that’s why multiple inheritance is not supported in java for e. g suppose there three classes are available A , B and C here A and B both are parent class and C is a chilled class. So if C class wants to access some property from A and B and suppose if both the class having same method like add() so here compiler will be confuse which class method should he access. So here compile time error will occur.

    1567100246-image.png