Jun 30, 2022

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 hybrid and multiple inheritance using interface only. 
In this post we are going to cover first three types of inheritance. So, let's learn and understand inheritance with some 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:
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 classes inherits a single class, then this is called as a hierarchical inheritance. In the below example we can clearly see that Child_1 and Child_2 classes extends single Parent class. So, using that both child class objects we can call 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



That's all about inheritance 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.... :)

No comments:

Post a Comment

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