Inheritance Example In Java

This section will describe you a simple example of Inheritance in Java.

Inheritance Example In Java

This section will describe you a simple example of Inheritance in Java.

Inheritance Example In Java

Inheritance Example In Java

In this section we will read about the Inheritance using a simple example.

Inheritance is an OOPs feature that allows to inherit the features of parent into the child. Inheritance allows the reusability of code that saves the time of programmer while coding. Technically, inheritance can be described as the derivation of one class from another class. In inheritance an object contains all the properties and behaviors of its parent class. An inheritance established the IS-A relationship between two objects.

Types of Inheritance

In the programming language there are various types of inheritance. These are as follows :

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance

But, when we talk about the Inheritance in Java the Multiple inheritance is not supported. This reduces the complexity and simplifies the language.

To apply the feature of inheritance in Java the keyword extends is used. Following syntax is used for applying the inheritance :

class className extends parentClassName {
.....
}

Example

Here I am giving a simple example which will demonstrate you about how to use the inheritance feature in Java programming. This example will demonstrate you the code reusability advantage of inheritance. In this example, you will see that we have created the various classes. In these classes the parent/super class is the class which is extend by the another class called base/child/derived class. The base class can acquire all the properties and behaviors of parent class and can use it without declaring or defining again inside the base class. In this example I have created a class named ClassA.java (this class will be the parent class) where I have declared the properties and defined the methods to set the properties values and display the result. Then I have created another class named ClassB.java (this class will extend the ClassA so, it will be the base class) where I have defined a method for adding two numbers, then I have created a main class where I have created an object of ClassB then I have used all the properties and methods of ClassA and then performed the add operation. Use of all the properties and behaviors of ClassA in main class using ClassB object specifies the reusability of code. Such as I didn't write the code two times for showing the addition result or declare the properties again.

ClassA.java

public class ClassA {
	
	int a;
	int b;
	int result;
	
	public void setValue(int a, int b)
	{
		this.a = a;
		this.b = b;
	}
	public void showResult()
	{
		System.out.println("Addition of two numbers = "+result);
	}
}

ClassB.java

public class ClassB extends ClassA {

	public void add()
	{
		result = a+b;
	}
}

MainClass.java

public class MainClass {

	public static void main(String args[])
	{
		ClassB b = new ClassB();
		b.setValue(6, 4);
		b.add();
		b.showResult();
	}
}

Output

When you will compile and execute the above class i.e. MainClass.java then the output will be as follows :

Download Source Code