Inheritance in Java with Examples

In this post we are going to learn what is inheritance in java and what are the types of inheritance in java with their examples.

Inheritance in java is a Object Oriented Programming(OOPS) concept, which allows one class to inherit all the non-private properties(fields and methods) of another class. We can implement inheritance using extends keyword. 

Types of Inheritance in Java:

Basically there are 5 types of inheritance. But, In Java using class we can achieve only three types of inheritance: single, multilevel and hierarchical. We can achieve multiple and hybrid inheritance using interfaces only. 
In this post, we are going to cover all 5 types of inheritance in Java. So, let's learn and understand what inheritance is, along with its definition and examples.



Example of Single Inheritance

If one class extends only one parent class then this is called as single inheritance. In the below example we can clearly see that class B inherits class A and using that class B we can easily access all data members present in class A. Here, A is a parent class of class B and B is a child class of  A. 
class A{
	int speed = 300;
}

class B extends A{
	void bike() {
		System.out.println("Speed of the bike is: "+speed);
	}
}

public class SingleIn {
		
	public static void main(String[] args) {
		//Creating the object of class B
		B obj = new B();
		obj.bike();
	}

}


OUTPUT: 
Speed of the bike is: 300




Example of Multilevel Inheritance:

Multilevel inheritance in Java means that one class can inherit from another class that already inherited from a different class.  It's like building blocks, where each class inherits and extends the features of the classes above it. In the following example we can see that Child class inherits the Father class and Father class inherits another class called GrandFather . So, this type of inheritance is called as multilevel inheritance.
class GrandFather{
	void love(){
	System.out.println("Loving their Grandchild.... ");
	}	
}
class Father extends GrandFather{
	void work(){
	System.out.println("Working.... ");
	}
	
}
class Child extends Father{
	void play(){
	System.out.println("Playing.... ");
	}
	
}
public class MultilevelIn {

	public static void main(String[] args) {
    	//using child class object we can happily access parent class methods
		Child obj = new Child();
		obj.play();
		obj.work();
		obj.love();
	}

}

OUTPUT: 
Playing.... 
Working.... 
Loving their Grandchild....



Example of Hierarchical Inheritance:

When more than one class inherits a single class, then it is called as hierarchical inheritance. In the example below, we can clearly see that the Child_1 and Child_2 classes extend a single Parent class. Therefore, by using objects of both child classes, we can call the parent class method.

class Parent{
	void property() {
		System.out.println("Parents Property");
	}
}
//Here child_1 and child_2 extends same parent class
class Child_1 extends Parent{
	void c1_property() {
		System.out.println("Child-1 Property");
	}
}
class Child_2 extends Parent{
	void c2_property() {
		System.out.println("Child-2 Property");
	}
}
public class HierarchicalIn{

	public static void main(String[] args) {
		
		Child_1  obj1 = new Child_1();
		obj1.property();
		obj1.c1_property();
		
		Child_2 obj2 = new Child_2();
		obj2.property();
		obj2.c2_property();
	}

}

OUTPUT:
Parents Property
Child-1 Property
Parents Property
Child-2 Property

  
These are the examples of single, multilevel and hierarchical inheritance in Java. However, as I mentioned, we can achieve hybrid and multiple inheritance in Java using interfaces only.  Now, let's explore what is hybrid and multiple inheritance in Java with some examples.


Example of Multiple Inheritance:

Multiple inheritance in Java refers to a situation where a class can inherit properties and behaviors from more than one parent classes. 
For example, let's consider two parent interfaces: Coder and Dancer. The Coder interface contains a method called coding(), while the Dancer interface contains a method called dancing(). Now, if we have class called Person that implements the Coder and Dancer interfaces, then we are allowed to provide the implementation for the coding() and dancing() methods in the Person class, as shown in the below example..
interface Coder {
    void coding();
}

interface Dancer {
    void dancing();
}

class Person implements Coder, Dancer {
    public void coding() {
        System.out.println("Person is coding...");
    }

    public void dancing() {
        System.out.println("Person is dancing...");
    }
}

public class Main {
    public static void main(String[] args) {
        Person person = new Person();
        person.coding();
        person.dancing();
        
    }
}
OUTPUT:
Person is coding...
Person is dancing...


Example of Hybrid Inheritance:

In Java, hybrid inheritance refers to a combination of different types of inheritance, such as single inheritance and multiple inheritance. Single inheritance allows a class to inherit from a single superclass, whereas multiple inheritance enables a class to implement multiple interfaces, and the combination of both is often referred to as "hybrid inheritance" in Java. 
// Superclass
class Human {
    void eat() {
        System.out.println("Human is eating...");
    }
}

// Interface 1
interface Coder {
    void coding();
}

// Interface 2
interface Dancer {
    void dancing();
}

// Subclass implementing single inheritance and multiple interfaces
class Person extends Human implements Coder, Dancer {
    public void coding() {
        System.out.println("Person is coding...");
    }

    public void dancing() {
        System.out.println("Person is dancing...");
    }
}

// Main Class
public class Main {
    public static void main(String[] args) {
        Person person = new Person();
        person.eat(); // Inherited from Human
        person.coding(); // Implemented from Coder interface
        person.dancing(); // Implemented from Dancer interface
    }
}

OUTPUT:
Human is eating...
Person is coding...
Person is dancing...


In the example above, the Person class inherits from the Human class using single inheritance. It also implements the Coder and Dancer interfaces using multiple inheritance of interfaces. The Person class overrides the eat() method inherited from Human and implements the coding() and dancing() methods from the Coder and Dancer interfaces, respectively.


That's all about inheritance in Java, definition and types with examples. If you like this simple examples of inheritance, then share it with your friends and If you have any doubts please discuss below in the comment section.... :)



[Related Questions]


Q) Why multiple inheritance is not supported in java using classes?

In Java, we can't use multiple inheritance with classes because it can cause a problem called the "diamond problem." This problem occurs when a class wants to inherit from two or more parent classes that  have a methods with the same name, it becomes confusing for the class to decide which method to use. To avoid this confusion and keep things simple, Java doesn't allow multiple inheritance with classes.

Q) Why ambiguity problem won't be there in interface java?

There is no ambiguity problem with interfaces in Java. This is because interfaces are declared without any code inside them; they only define the method signatures. The actual implementation of these methods is provided by the class that implements the interface. This way, there is no confusion or conflict regarding which method implementation to use, as it is explicitly provided by the implementing class.

No comments:

Post a Comment

If you have any doubts, please discuss here...👇