Inheritance in Java is the way of code reusability this provides the facility to establish a relation between two classes as parent-child or base-derived. In this relation base will provides its non-private data and functions to the derived class.

  • Subclass  /Child – This class that inherits from another class.
  • Superclass  /Parent – This class being inherited from.

Extends Keywords

This keyword is use for making a single inheritance between two classes. Here, one class will be base class and other will be derived class.

Syntax of Inheritance

class derived-class extends base-class  
{  
   //methods and fields  
}  

Types of Inheritance in Java

  • Single Inheritance
  • Multi-level Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance

Notes: Multiple inheritance and Hybrid inheritance is not support in Java.

Single Inheritance

A subclasses inherit of one superclass, it is know as Single Inheritance.

Example of Single Inheritance

Input Value

class inhertance{  
    void study(){System.out.println("basic");}  
    }  
    class key extends inhertance{  
    void tus(){System.out.println("engineer");}  
    }  
    class TestInheritance{  
    public static void main(String args[]){  
    key d=new key();  
    d.study();  
    d.tus();  
    }} 
Example of Single Inheritance

Let’s run the code

Input Value

Output Value

Output Value

Multi-level Inheritance

A derived class will be inheriting a base class, and as well as the derived class also acts as the base class for other classes.

Example of Multi-level Inheritance

Input Value

class inhertancem{  
    void study(){System.out.println("basic");}  
    }  
    class tur extends inhertancem{  
    void key(){System.out.println("engineer");}  
    }  
    class Babytur extends tur{  
    void menu(){System.out.println("Hello Student");}  
    }  
    class inhertancem2{  
    public static void main(String args[]){  
    Babytur d=new Babytur();  
    d.menu();  
    d.key();  
    d.study();  
    }}  
Example of Multi-level Inheritance

Let’s run the code

Input Value

Output Value

Output Value

Hierarchical Inheritance

A one class serves as a superclass for more than one subclass, It is know as Hierarchical Inheritance.

Example of Hierarchical Inheritance

Input Value

class inhertainh{  
    void study(){System.out.println("Basic");}  
    }  
    class rose extends inhertainh{  
    void key(){System.out.println("Engineer");}  
    }  
    class lily extends inhertainh{  
    void tur(){System.out.println("Hello Student");}  
    }  
    class TestInheritance3{  
    public static void main(String args[]){  
    lily c=new lily();  
    c.tur();  
    c.study();  
    }}  
Example of Hierarchical Inheritance

Let’s run the code

Input Value

Output Value

Output Value

Constructor With Inheritance

If the base and derived both classes having constructor then constructor never be inherited means base class constructor never be inherited with derived class but derived class constructor can call the base class constructor automatically.

Example Constructor With Inheritance

Input Value

class staticm
{
    public staticm ()
    {
        System.out.println("Basic Engineer");
    }
}
class Con_inher extends staticm
{
    public Con_inher()
    {
        System.out.println("Basic Engineer");
    }
    public static void main(String[] args) {
        Con_inher m=new Con_inher();
    }
}
Example Constructor With Inheritance

Let’s run the code

Input Value

Output Value

Output Value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *