Java Inheritance - The Coding Shala

Home >> Learn Java >> Java Inheritance

Java Inheritance

Java Inheritance is an important part of OOPs. Inheritance is a mechanism in which one class(object) acquires all the properties and behaviors (fields and methods) of another class(parent). When we create a class A, now we want to create another class and want to write the same method of class A so instead of writing the same methods again in the class we inherit methods of class A, this is called Inheritance. Using Inheritance we can reuse the code. Inheritance represents the IS-A relationship, also known as a parent-child relationship.

The following are important terms used in Inheritance:
  • Child Class: Also known as a derived class, extended class or subclass. The child class is a class that inherits the other class.
  • Parent Class: Also known as superclass or base class. A superclass is a class from where a subclass inherits the features.
  • Reusability: As the name suggests, we can reuse the fields and methods of the existing class when we create a new class. Java Inheritance is also known for code reusability.

The syntax for Java Inheritance

We use the 'extends' keyword for inheritance. It means when we create a new class it inherits properties from the parent class. The meaning of 'extends' is to increase functionality.
class childclass extends parentclas{
    //methods
}

Example of Java Inheritance

The following example explains the Java Inheritance:


class ParentClass{
    int classid = 1;
    void Display(){
        System.out.println("This is parent class with id: "+ classid);
    }
}

class ChildClass extends ParentClass{
    int classid = 2;
    void Dis(){
        System.out.println("This is child class with id: "+ classid);
    }
    
    public static void main(String[] args){
        ChildClass cc = new ChildClass();
        cc.Display(); //cc is child class object inherits paretnclass Display()
        cc.Dis(); 
    }
}
Output:
This is parent class with id: 1
This is child class with id: 2

Types of Inheritance

The following are different types of Java Inheritance:
  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance
Note: Java does not support Multiple and Hybrid Inheritance with classes. Multiple and Hybrid inheritance can only be achieved only through Interfaces.

Single Inheritance

If a child class inherits the features of only one superclass is known as single inheritance.
Java Single Inheritance - The Coding Shala

Example of Single Inheritance in Java

The following example explains the single inheritance:
class Fruits{  //parent class 
    void PrintF(){
        System.out.println("This box contains Fruits");
    }
}

class Apple extends Fruits{ //child class 
    void PrintA(){
        System.out.println("This is an Apple");
    }
}

class Main{
    public static void main(String[] args){
        Apple a = new Apple();
        a.PrintF();
        a.PrintA();
    }
}
Output:
This box contains Fruits
This is an Apple

Multilevel Inheritance

If a child class inherits the features of a base class and as well as child class also acts as a base class for another class is called multilevel inheritance. The below image explains multilevel inheritance:


Java Multilevel Inheritance - The Coding Shala

Example of Multilevel Inheritance in Java

The following example explains the multilevel Inheritance:
class A{ //this is Grandparent
    void PrintA(){
        System.out.println("This is class A");
    }
}

class B extends A{ //this is parent class 
    void PrintB(){
        System.out.println("This is class B");
    }
}

class C extends B{ //this is child class 
    void PrintC(){
        System.out.println("This is class C");
    }
}

class Main{
    public static void main(String[] args){
        C cc = new C();
        cc.PrintA();
        cc.PrintB();
        cc.PrintC();
    }
}
Output:
This is class A
This is class B
This is class C

Note: In Java, a class cannot directly access the grandparent's members. A class only can directly access it's parent's members. The following example explains how to access grandparent's member in Java:
class Grandparent{
    void Display(){
        System.out.println("This is Grandparent");
    }
}

class Parent extends Grandparent{
    void Display(){
        super.Display();
        System.out.println("This is Parent");
    }
}

class Child extends Parent{
    void Display(){
        super.Display();
        //super.super.Display(); //this will give compile error
        System.out.println("This is child");
    }
}

class Main{
    public static void main(String[] args){
        Child c = new Child();
        c.Display();
    }
}
Output:
This is Grandparent
This is Parent
This is child

Hierarchical Inheritance

In Hierarchical Inheritance, more than one subclasses inherit the features from a single parent class. The following image explains hierarchical inheritance:
Java Hierarchical Inheritance - The Coding Shala

Example of Hierarchical Inheritance

The example of Hierarchical Inheritance is as follows:
class A{
    void PrintA(){
        System.out.println("This is class A");
    }
}

class B extends A{
    void PrintB(){
        System.out.println("This is class B");
    }
}

class C extends A{
    void PrintC(){
        System.out.println("This is class C");
    }
}

class Main{
    public static void main(String[] args){
        C c = new C();
        c.PrintC();
        //c.PrintB(); //this will give error 
        c.PrintA();
        
        B b = new B();
        b.PrintA();
    }
}
Output:
This is class C
This is class A
This is class A

Why multiple inheritances are not supported in Java?
Java does not support multiple inheritances to reduce the complexity and simplify the language. Multiple inheritances are only achieved by interfaces in Java. The following example explains why multiple inheritances are not supported in Java:
class A{
    void Display(){
        System.out.println("Class A");
    }
}

class B{
    void Display(){ //same as class A 
        System.out.println("Class B");
    }
}

class C extends A,B{ //if possible
    public static void main(String[] args){
        C c = new C();
        c.Display(); // now which Display method is going to call
        //it creates ambiguity here 
        //that's why multiple inheritance is not supported by java with classes.
    }
}

Important Points about Inheritance in Java
The following are some important points about inheritance:
  • Every child class has one and only one direct superclass.
  • A child class inherits all the members(fields, methods) from its superclass. Constructors are not members, so they are not inherited by subclasses, but they can be invoked from the subclass.
  • A child class does not inherit the private members of its parent class.
  • We can use inherited fields and methods in child class and also can write a new one also that is not in the superclass.
  • During inheritance, the only object of child class is created not the superclass. The following example explains it:
class A{
    A(){
        System.out.println("Super Class Constructor with hash code: "+ this.hashCode());
    }
}

class B extends A{
    B(){
        System.out.println("Child class constructor with hash Code: "+ this.hashCode());
    }
}

class Main{
    public static void main(String[] args){
        new B();
    }
}
Output:
Super Class Constructor with hash code: 589431969
Child class constructor with hash Code: 589431969

Here in this example hash Code of superclass and subclass is the same that means only one object is created for the child class. Hash Code is a unique id number allocated to an Object by JVM.


Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Single Number 3 LeetCode Solution - The Coding Shala

LeetCode - Number of Good Pairs Solution - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Java Method Overloading - The Coding Shala