Inheritance in Java-Types and Examples

Inheritance in Java is the process of acquiring the properties of one class object into another class object.

It allows the hierarchical classification. Inheritance also provides the facility of reusability.

In Java programming, a class that is inherited is called a superclass or sub class or parent class and class that does the inheriting is called sub class or child class.     

If we want to inherit a class, we simply incorporate the definition of one class into another class by using extends keyword.

General form of a class declaration that inherits a super class

 
class  subClass extends   superClass{ 
 
  }

Type of Inheritance in Java

There are following type of inheritance

  1. Single level Inheritance
  2. Multi-level Inheritance
  3. Hierarchical Inheritance
  4. Hybrid Inheritance

Note: Java does not support multiple inheritance.        

Single level Inheritance

Property of single level inheritance is as below

Single level Inheritance in Java
Fig:java single inheritance
  • One Super class and one subclass
  • Subclass inherits the features of superclass

Write a program for single-level inheritance.

 
class Parent // create a super class	
{	
    int  x ,y;
    void showA()
    {       
        System.out.println("x="  +x+" y=" + y); 
    }		
   }


class Child extends Parent // create a sub class	
{	
    int  z; 
    void showB()
    {   
        System.out.println("x="  +x + "  y=" + y + " z=" +z );
    }
/* in sub class, member of super class can be accessed directly without any object. */                     }


public class Test {

    public static void main(String arg[]) {
        Child obj = new Child();
        obj.x = 10;	// subclass has access to all public members of its super class	
        obj.y =  20;	// subclass has access to all public members of its super class	
        obj.showA();	// subclass has access to all public members of its super class	
        obj.z = 30;
        obj.showB();
    }
}

Output

 
x=10 y=20
x=10  y=20 z=30

Multi-level Inheritance

In multi level inheritance inheritance exists in multiple level.

Multi level Inheritance  in Java
Multilevel Inheritance
  • One Parent Class
  • It has a child class
  • Child has again child class

This process may continue and each child class inherits feature of all parent class.

program for Multi level inheritance in Java

 
class A { 

    int x;

}

class B extends A {

    int y;
}

class C extends B {

    int z;

    void show() {
        System.out.println("x=" + x + "  y=" + y + " z=" + z);
        /* in sub class, member of super class can be accessed directly without any object. */

    }
}

public class Test {

    public static void main(String arg[]) {
        C objC = new C();
        objC.x = 10;	// subclass has access to all public members of its super class
        objC.y = 20;	// subclass has access to all public members of its super class
        objC.z = 30;
        objC.show();	// subclass has access to all public members of its super class	
    }
}


Result

 
x=10  y=20 z=30

Hierarchical Inheritance

One Parent class can have multiple child class this type of inheritance is known as Hierarchical Inheritance.

  • One Parent class
  • Multiple Child class
  • Each Child Inherits the feature of Parent
hierarchical inheritance in Java
java hierarchical inheritance

Write a program for Hierarchical Inheritance.

 
class A {

    int x;
}

class B extends A {

    int y;

    void showB() {
        System.out.println("x=" + x + "  y=" + y);
        /* in sub class, member of super class can be accessed directly without any object. */
    }
}

class C extends A {

    int z;

    void showC() {
        System.out.println("x=" + x + "  z=" + z);
        /* in sub class, member of super class can be accessed directly without any object. */
    }
}

public class Test {

    public static void main(String arg[]) {
        B objB = new B();
        C objC = new C();
        objB.x = 10;	// subclass has access to all public members of its super class	
        objB.y = 20;	// subclass has access to all public members of its super class				
        objC.x = 100;	// subclass has access to all public members of its super class		
        objC.z = 200;	// subclass has access to all public members of its super class				
        objB.showB();	// subclass has access to all public members of its super class
        objC.showC();	// subclass has access to all public members of its super class	
    }
}



Result

 
x=10  y=20
x=100  z=200



Hybrid  Inheritance

Combination of two or more Inheritance type is known as Hybrid Inheritance

Hybrid  Inheritance in Java
java Hybrid Inheritance
 
class A { 
 
    int x;
 
}
 
class B extends A {
 
    int y;
	 void show() {
        System.out.println("x=" + x + "  y=" + y );
        /* in sub class, member of super class can be accessed directly without any object. */
 
    }
}
 
class C extends A {
 
    int z;
 
    void show() {
        System.out.println("x=" + x + " z=" + z);
        /* in sub class, member of super class can be accessed directly without any object. */
 
    }
}
class D extends B {
 
    int a;
 
    void show() {
        System.out.println("x=" + x +" y ="+y +" a=" + a);
        /* in sub class, member of super class can be accessed directly without any object. */
 
    }
}

 
public class Test3 {
 
    public static void main(String arg[]) {
		D d=new D();
		d.x=30;
		d.y=20;
		d.a=10;
		d.show();
		
        C objC = new C();
        objC.x = 10; // subclass has access to all public members of its super class
        objC.z = 30;
        objC.show(); // subclass has access to all public members of its super class 
    }
}

Note: A subclass can access the public member of super class. A subclass can’t access the private member of super class.

 
javac Test3.java

java Test3
x=30 y =20 a=10
x=10 z=30

Super Class Cant Access Sub Class Member

A subclass can access all the public members of the super class but the reverse is not true. The super class cannot access sub-class data.

 
class Parent // create a super class	
{

    int x;

    void showParent() {
        System.out.println("x =" + x + "  y=" + y);
        /* error, y cant be accessed here, because y is a member of sub class. */
    }
}

class Child extends Parent // create a sub class
{

    int y;

    void showChild() {
         System.out.println("x =" + x + "  y=" + y);
    }
    /* in sub class, member of super class can be accessed directly without any object. */
}

public class TestInheritance {

    public static void main(String arg[]) {
        Child obj = new Child();
        obj.x = 10;	// subclass has access to all public members of its super class	
        obj.y = 20;	// subclass has access to all public members of its super class
        obj.showParent();	// subclass has access to all public members of its super class	
        obj.showChild();
    }
}

Result

 
error: cannot find symbol
        System.out.println("x =" + x + "  y=" + y);
                                                ^
  symbol:   variable y
  location: class Parent
1 error

A subclass can’t access the private member of superclass.   

 
class A { // super class	

    private int x;
}

class B extends A {// sub class	

    int y;

    void showB() {
        System.out.println("x & y =" + x + "    " + y); //error 
        /*  sub class, can’t access private member its super class . */
    }

}

public class Test11 {

    public static void main(String args9[]) {
        B objB = new B();
        objB.showB();

    }
}


Result

 
Test11.java:11: error: x has private access in A
        System.out.println("x=" + x + "   y=" + y); //error 
                                       ^
1 error

Read More

Multithreading in Java Thread Creation Method Use Example

Method Overloading with examples in Java