Inheritance in Java

Single Inheritance

When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.

    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

When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.

    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

When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is 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("meowing...");}  
    }  
    class TestInheritance3{  
    public static void main(String args[]){  
    Cat c=new Cat();  
    c.meow();  
    c.eat();  
    //c.bark();//C.T.Error  
    }}  

Output:

meowing...
eating...

Difference between super() and this() in java Program

Sr. No.Keythissuper
1Represent and Referencethis keyword mainly represents the current instance of a class.On other hand super keyword represents the current instance of a parent class.
2Interaction with class constructorthis keyword used to call default constructor of the same class.super keyword used to call default constructor of the parent class.
3Method accessibilitythis keyword used to access methods of the current class as it has reference of current class.One can access the method of parent class with the help of super keyword.
class A {
   public int x, y;
   public A(int x, int y) {
      this.x = x;
      this.y = y;
   }
}
class B extends A {
   public int x, y;
   public B() {
      this(0, 0);
   }
   public B(int x, int y) {
      super(x + 1, y + 1);// calls parent class constructor
      this.x = x;
      this.y = y;
   }
   public void print() {
      System.out.println("Base class : {" + x + ", " + y + "}");
      System.out.println("Super class : {" + super.x + ", " + super.y + "}");
   }
}
class Point {
   public static void main(String[] args) {
      B obj = new B();
      obj.print();
      obj = new B(1, 2);
      obj.print();
   }
}

Output

Base class : {0, 0}
Super class : {1, 1}

Base class : {1, 2}
Super class : {2, 3}

reference:

https://www.geeksforgeeks.org/inheritance-in-java/

https://www.javatpoint.com/inheritance-in-java

https://www.geeksforgeeks.org/super-and-this-keywords-in-java/

https://www.tutorialspoint.com/difference-between-super-and-this-in-java-program

Leave a comment

Blog at WordPress.com.

Design a site like this with WordPress.com
Get started