Inheritance in Java Programming

Inheritance in Java Programming

Inheritance in Java Programming is one of the features of object-oriented programming. It allows a class to have properties and behavior of other class in it. Thus, it allows code reusability. The class inheriting properties and methods of a class is the child class and the class being inherited is called the parent class. Inheritance allows classes to have a parent-child relationship.

Syntax of Java Inheritance:

class SubClassName extends SuperClassName{
//data members and member functions
}

To inherit a class, extends keyword is used. Parent class is also known as Super class and child class is also known as Sub class.

 

 

Why Inheritance?

  • For method overriding which makes achieving runtime-polymorphism possible.
  • For code reusability.

 

Types of Inheritance in Java

There are three types of inheritance on the basis of class in Java which are:

    • Single Inheritance

    • Multilevel Inheritance

    • Hierarchical Inheritance

 

There are two types of inheritance on the basis of the interface in Java which are:

    • Multiple Inheritance

    • Hybrid Inheritance

 

 

Single Inheritance

When a number of parent and child classes is 1 then it is single inheritance.

Example of Single Inheritance:

//Java Program with Single Inheritance
//Parent Class
class Parent{
    int var_p=23;
}

//Child Class
class Child extends Parent{
    int var_c=78;
}

public class SingleInheritance {
    public static void main(String[] args) {
        Child c=new Child();
        System.out.println("Accessing Parent's member: "+c.var_p);
        System.out.println("Accessing It's member: "+c.var_c);
    }
}

Output:

Accessing Parent's member: 23
Accessing It's member: 78

 

 

Multilevel Inheritance

This inheritance involves a chain of inheritance where a child to a class is a parent to another class and so on.

Example of Multilevel Inheritance:

//Java Program with multilevel inheritance
class A{
    int var_a=67;
}

class B extends A{
    int var_b=78;
}

class C extends B{
    int var_c=49;
}
public class MultilevelInheritance {
 public static void main(String[] args) {

    //object instantiation of class C
     C c=new C();

     //object of C can access members of class A and class B
     System.out.println(c.var_a);
     System.out.println(c.var_b);
     System.out.println(c.var_c);
 }   
}

Output:

67
78
49

 

 

Hierarchical Inheritance

This inheritance involves one parent class and two or more child classes. It allows more than one class to inherit the properties and behavior of one class, the parent class.

Example of Hierarchical Inheritance:

//Java Program with hierarchical inheritance

class Parent{
    int var_p=1;
}

class Child1 extends Parent{
    int var_c1=2;
}

class Child2 extends Parent{
    int var_c2=3;
}

public class HierarchicalInheritance {
    public static void main(String[] args) {
        //Both classes Child1 and Child2 extends class Parent
        Child1 c1=new Child1();
        System.out.println("Accessing Parent's member: "+c1.var_p);
        System.out.println("Accessing It's member: "+c1.var_c1);

        Child2 c2=new Child2();
        System.out.println("Accessing Parent's member: "+c2.var_p);
        System.out.println("Accessing It's member: "+c2.var_c2);
    }
}

Output:

Accessing Parent's member: 1
Accessing It's member: 2    
Accessing Parent's member: 1
Accessing It's member: 3

 

 

Why Multiple Inheritance through Classes is not supported in Java?

Multiple inheritance is not supported in Java to reduce complexity and simplify the code. It leads to code ambiguity. Assume a scenario where class C has two parents, class A and class B, and both of them a method with the same name show(). When an object of class C is instantiated and is used to call function show(), then it will lead to ambiguity as the interpreter will have no way to identify to which class the method show() belongs to that we are trying to invoke and will result in a compile-time error.

 

 

Share this post

Leave a Reply

Your email address will not be published.