Inheritance in java

Inheritance is the process by which objects of one class can link and share some common properties of objects from another class.

Use of inheritance :

  • For Method Overriding (so runtime polymorphism can be achieved).
  • For Code Reusability.

Syntax of inheritance :

class Subclass-name extends Superclass-name
 {
     //methods and fields
 }

The extends keyword indicates that you are making a new class that derives from an existing class.

There are various types of inheritance:

  1. Single Inheritance
  2. Multileveled Inheritance
  3. Hierarchical Inheritance
  4. Multiple Inheritance
  5. Hybrid Inheritance
  1. Single Inheritance : One class extends another class.

Example :

/* JAVA program to understand Single Inheritance */
/* www.codewindow.in */

class A
{
    void showA()
    {
        System.out.print(“A class method”);
    }
}
Class B extends A
{
    void showB()
    {
        System.out.print(“B class method”);
    }
public static void main(String args[])
{
    A ob=new A();
    Ob.showA();
    B ob1=new B();
    Ob1.showA();
    Ob1.showB();
    
}
}

Output

A class method
A class method
B class method

2. Multileveled Inheritance: One class inherits from another class, then another class can also be extended from derived class, in that case derived class becomes base class.

Example :

/* JAVA program to understand Multileveled Inheritance */
/* www.codewindow.in */

public class A
{
    void showA()
    {
        System.out.print(“A class method”);
    }
}
public class B extends A
{
    void showB()
    {
        System.out.print(“B class method”);
    }
}
public Class C extends B
{
    void showC()
    {
        System.out.print(“C class method”);
    }
public static void main(String args[])
{
    A ob=new A();
    Ob.showA();
    B ob1=new B();
    Ob1.showA();
    Ob1.showB();
    C ob2=new C();
    Ob2.showA();
    Ob2.showB();
    Ob2.showC();
}
}

Output

A class method
A class method
B class method
A class method
B class method
C class method

3. Hierarchical Inheritance : In this type of inheritance, one class is inherited by many sub-class.

Example :

/* JAVA program to understand Hierarchical Inheritance */
/* www.codewindow.in */

public class A
{
    void showA()
    {
        System.out.print(“A class method”);
    }
}
public class B extends A
{
    void showB()
    {
        System.out.print(“B class method”);
    }
}
public Class C extends A
{
    void showC()
    {
        System.out.print(“C class method”);
    }
public static void main(String args[])
{
    A ob=new A();
    Ob.showA();
    B ob1=new B();
    Ob1.showA();
    Ob1.showB();
    C ob2=new C();
    Ob2.showA();
    Ob2.showC();
}
}

Output

A class method
A class method
B class method
A class method
C class method

4. Multiple Inheritance: In this type of inheritance one class inherits from more than one parent class.

Multiple inheritance is not allowed here, in that case, it causes ambiguity error. If we create an object in C, the methods and properties of A and B will inherit in C class. So when we call it will get confused, if it will call a method of class A or B, it is called an ambiguity error.

5. Hybrid inheritance: It is a combination of multiple and hierarchical inheritance. Java doesn’t support hybrid inheritance because it causes ambiguity error.

Latest Post

Archives

Leave a Comment

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