Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Types of inheritance

Java Programming Language Types of Inheritance
Single inheritance

Single inheritance takes place in between only one superclass and one subclass.

Java Programming Language Single Inheritance

In the above figure of single inheritance, A is a superclass and B is its subclass.
The B class inherits the properties of A class.

Example of single inheritance

Here we have taken the Father as a superclass and Son as a subclass.

class Father 
{
    int Father_Bank_bal = 2000;
	
    public void fathersBalance() 
    {
        System.out.println("Father's Bank balance :: "+Father_Bank_bal);
    }
}

class Son extends Father
{
    int Son_Bank_bal = 1000 + Father_Bank_bal;
	
    public void sonsBalance() 
    {
        System.out.println("Son's Bank balance :: "+Son_Bank_bal);
    }
}

class SingleInheritance 
{
    public static void main(String args[])
    {
        Father f = new Father();
        Son s = new Son();
        f.fathersBalance();
        System.out.println("----------------------------------");
        s.sonsBalance();		
    }
}

Output

Father's Bank balance :: 2000
----------------------------------
Son's Bank balance :: 3000

Multilevel inheritance

We can create hierarchies that contain as many layers of inheritance as we want. It is perfectly valid to a subclass as a superclass of another class.

Java Programming Language Multilevel Inheritance

In the above figure of multilevel inheritance, A is a superclass of B. Therefore B acts as a subclass of A.
Even though B is a subclass of A it acts as a superclass of C. That is A is a superclass of B, and B is a superclass of C.

Here C inherits members of A as well as B excluding private members.

Example of Multilevel Inheritance

Here we have taken GrandFather as a superclass of class Father and Father as a superclass of class Son.

class GrandFather 
{
    int GrandFather_Bank_bal = 3000;
	
    public void grandFathersBalance() 
    {
        System.out.println("Grandfather's Bank balance :: "+GrandFather_Bank_bal);
    }
}

class Father extends GrandFather
{
    int Father_Bank_bal = 2000 + GrandFather_Bank_bal;
	
    public void fathersBalance() 
    {
        System.out.println("Father's Bank balance :: "+Father_Bank_bal);
    }
}

class Son extends Father 
{
    int Son_Bank_bal = 1000 + Father_Bank_bal;
	
    public void sonsBalance() 
    {
        System.out.println("Son's Bank balance :: "+Son_Bank_bal);
    }
}

class MultilevelInheritance 
{
    public static void main(String args[])
    {
        GrandFather gf = new GrandFather();
        Father f = new Father();
        Son s = new Son();      
        gf.grandFathersBalance();
        System.out.println("----------------------------------");
        f.fathersBalance();
        System.out.println("----------------------------------");
        s.sonsBalance();		
    }
}

Output

Grandfather's Bank balance :: 3000
----------------------------------
Father's Bank balance :: 5000
----------------------------------
Son's Bank balance :: 6000

Hierarchical Inheritance

Hierarchical inheritance means one superclass can be inherited by more than one subclass.

Java Programming Language Hierarchical Inheritance

In the above figure of hierarchical inheritance, A is a superclass for class B, C and D. That is class B, C and D each inherits same properties/members of class A.

Example of hierarchical inheritance

Here we have taken the class Employee as a superclass of classes SoftwareDeveloper and SoftwareTester.

class Employee 
{
    int salary;
}

class SoftwareDeveloper extends Employee
{
    public SoftwareDeveloper(int i) 
    {
        salary = i;
    }
	
    public void displaySalary()
    {
        System.out.println("Software Developer's salary in US$ :: "+salary);
    }
}

class SoftwareTester extends Employee 
{
    public SoftwareTester(int i) 
    {
        salary = i;
    }
	
    public void displaySalary()
    {
        System.out.println("Software Tester's salary in US$ :: "+salary);
    }
}

class Hierarchical_Inher 
{
    public static void main(String args[])
    {
        SoftwareDeveloper d = new SoftwareDeveloper(90000);
        SoftwareTester t = new SoftwareTester(80000);
        d.displaySalary();
        System.out.println("-----------------------------------------");
        t.displaySalary();
    }
}

Output

Software Developer's salary in US$ :: 90000
-----------------------------------------
Software Tester's salary in US$ :: 80000

Multiple Inheritance

In multiple inheritance, one subclass inherits two or more superclasses.
This type of inheritance is not allowed in Java.

Java Programming Language Multiple Inheritance

Not allowed in Java

Java Programming Language Multiple Inheritance Example

Reason

Multiple inheritance is not allowed in Java to avoid the complexity in programs, like ambiguity to call fields or methods of same names of different classes.

 

Leave a reply