Inheritance in Java | Types of Inheritance in Java | PLM Developer

Inheritance In Java

 

Inheritance means acquiring the properties from the super class. The main objective of Inheritance is to achieve code re-usability. Inheritance plays important role OOPS (Object Oriented Programming System).

Inheritance is the way to reuse the existing methods and fields of the super class. In Java inheritance is implemented using extends keyword.

Types of Inheritance:

1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance

Single Inheritance:

In single Inheritance, one class(subclass) extends the properties of another class (one parent class). In the below example class B extending the properties of class A.

Multiple Inheritance:

In Multiple Inheritance, one class(subclass) extends the properties of multiple classes(multiple parent classes). Java doesn’t support Multiple Inheritance throw classes, But we can achieve by using interfaces.  In the below class C extending the properties of class A and class B.

Multilevel Inheritance:

In Multilevel Inheritance, In the blow class C extending the properties of class B and class B extending the properties of class A.

Hierarchical Inheritance:

In Hierarchical Inheritance, multiples subclasses extend the properties of a single class. In the below class B, class C, class D extending the properties of class A.

Example for Single Inheritance:

//Exapmle Java program for Single Inheritance 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class Parent
{
    public void parentMethod()
    {
        System.out.println(“From Parent Class”);
    }
}
 
class Child extends Parent
{
    public void childMethod()
    {
        System.out.println(“From Child Class”);
    }
}
// Main class
public class Main
{
    public static void main(String[] args)
    {
        Child child = new Child();
        child.childMethod();
        child.parentMethod();
    }
}

Example for Multiple Inheritance:

//Example Java program for Multiple inheritance
import java.util.*;
import java.lang.*;
import java.io.*;
  
interface Interface1
{
    public void Interface1Method();
}
  
interface Interface2 extends Interface1
{
    public void Interface2Method();
}
class ChildClass implements Interface2
{
    public void Interface1Method() {
        System.out.println(“From Interface1”);
    }
  
    public void Interface2Method()
    {
        System.out.println(“From Interface2”);
    }
}

// Drived class
public class Main
{
    public static void main(String[] args)
    {
        ChildClass child = new ChildClass();
        child.Interface1Method();
        child.Interface2Method();
    }
}

Example for Multilevel Inheritance:

import java.util.*;
import java.lang.*;
import java.io.*;
 
class Parent1
{
    public void parent1Methid()
    {
        System.out.println(“From Parent1”);
    }
}
 
class Parent2 extends Parent1
{
    public void parent2Methid()
    {
        System.out.println(“From Parent2”);
    }
}
 
class Child extends Parent2
{
    public void parent3Methid()
    {
        System.out.println(“From Parent3”);
    }
}
 
// Drived class
public class Main
{
    public static void main(String[] args)
    {
        Child child = new Child();
        child.parent1Methid();
        child.parent2Methid();
        child.parent3Methid();
    }
}

Example for Hierarchical Inheritance:

// Example for Java program for Hierarchical Inheritance
import java.util.*;
import java.lang.*;
import java.io.*;
 
class Parent
{
    public void parentMethod()
    {
        System.out.println(“From Parent”);
    }
}
 
class Child1 extends Parent
{
    public void childMethod1()
    {
        System.out.println(“From Child1”);
    }
}
 
class Child2 extends Parent
{
   public void childMethod1()
    {
        System.out.println(“From Child2”);
    }
}
 
// Drived class
public class Main
{
    public static void main(String[] args)
    {
        Child2 child2= new Child2();
        child2.parentMethod();
        Child1 child1 = new Child1();
        child1.parentMethod();
        child1.childMethod1();
    }
}

Leave a Comment

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

Scroll to Top