Inheritance in Java

HOME

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another class.

The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

Inheritance is applicable for public and protected members only. Private members can’t be inherited.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

The keyword used for inheritance is extends

Syntax

class derived-class extends base class
{
//methods and variables
}

Below is an example of Inheritance. In the below example, ParentCalculation is the Base class (parent class) and has a method addition. ChildCaclculation is the derived class (child class) which has extends base class ParentCalculation. When an object of ChildCaclculation class is created, a copy of all methods and fields of the superclass acquire memory in this object. That is why by using the object of the subclass we can also access the members of a superclass. 

Base Class

public class ParentCalculation {

	int z;

	public void addition(int x, int y) {
		z = x + y;
		System.out.println("The sum of the given numbers:" + z);
	}

}

Derived Class

public class ChildCaclculation extends ParentCalculation {

	int c;

	public void multiplication(int a, int b) {
		c = a * b;
		System.out.println("The multiplication of the given numbers:" + c);
	}

	public static void main(String[] args) {
		ChildCaclculation cal = new ChildCaclculation();
		cal.addition(10, 14);
		cal.multiplication(5, 6);

	}

}

Output

The sum of the given numbers:24
The multiplication of the given numbers:30

Types of Inheritance in Java

Below are the different types of inheritance available in Java. 

1.Single Inheritance

In single inheritance, subclass (derived) inherit the features of superclass (base). In the image below, class A serves as a base class for the derived class B.

Base Class

public class Employee {

	String name = "Tom";
	int emp_no = 12001;

	public void displayEmployee() {
		System.out.println("Base Class");

		System.out.println("Name :" + name);
		System.out.println("Emp_No :" + emp_no);
	}
}

Derived Class

package JavaDemo.Inheritance;

public class Department extends Employee {

	String deptName = "IT Division";

	public void displayDept() {
		System.out.println("Derived Class");

		System.out.println("Department Name :" + deptName);
	}

	public static void main(String[] args) {

		Department dept = new Department();

		// Derived method
		dept.displayDept();

		// Base Method
		dept.displayEmployee();

	}

}

Output

Derived Class
Department Name :IT Division
Base Class
Name :Tom
Emp_No :12001

2. Multilevel Inheritance

In this, derived class extends an base class as well as the derived class act as a base class for another derived class. In the below example, Employee is Base class, Department is intermediate derived class and SubDivision is derived class.

Base Class

package JavaDemo.Inheritance;

public class Employee {

	String name = "Tom";
	int emp_no = 12001;

	public void displayEmployee() {
		System.out.println("Base Class");

		System.out.println("Name :" + name);
		System.out.println("Emp_No :" + emp_no);
	}
}

Intermediate Derived Class

package JavaDemo.Inheritance;

public class Department extends Employee {

	String deptName = "IT Division";

	public void displayDept() {
		System.out.println("Intermediate Derived Class");

		System.out.println("Department Name :" + deptName);
	}

	public static void main(String[] args) {

		Department dept = new Department();

		// Intermediate Derived method
		dept.displayDept();

		// Base Method
		dept.displayEmployee();

	}

}

Derived Class

package JavaDemo.Inheritance;

public class SubDivision extends Department {

	String sub_division = "Production Support";

	public void displaySubDivision() {
		System.out.println("Derived Class");

		System.out.println("SubDivision Name :" + sub_division);
	}

	public static void main(String[] args) {

		SubDivision div = new SubDivision();

		// Derived method
		div.displaySubDivision();

		// Intermediate Derived method
		div.displayDept();

		// Base Method
		div.displayEmployee();

	}

}

Output

Derived Class
SubDivision Name :Production Support
Intermediate Derived Class
Department Name :IT Division
Base Class
Name :Tom
Emp_No :12001

3.  Hierarchical Inheritance

In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclasses.

In the below example, Employee is the base class. Department and Salary are the derived classes.

Base Class

package JavaDemo.Inheritance;

public class Employee {

	String name = "Tom";
	int emp_no = 12001;

	public void displayEmployee() {
		System.out.println("Base Class");

		System.out.println("Name :" + name);
		System.out.println("Emp_No :" + emp_no);
	}
}

Derived Class1

public class Department extends Employee {

	String deptName = "IT Division";

	public void displayDept() {
		System.out.println("Derived Class 1");

		System.out.println("Department Name :" + deptName);
	}

	public static void main(String[] args) {

		Department dept = new Department();

		// Derived method
		dept.displayDept();

		// Base Method
		dept.displayEmployee();

	}

}

Derived Class2

package JavaDemo.Inheritance;

public class Salary extends Employee {

	float Salary = 65000;

	public void displaySalary() {
		System.out.println("Derived Class 2");
		System.out.println("Salary :" + Salary);
	}

	public static void main(String[] args) {

		Salary sal = new Salary();
		sal.displaySalary();
		sal.displayEmployee();

		System.out.println("=========================");

		Department dept = new Department();
		dept.displayDept();
		dept.displayEmployee();

	}

}

Output

Derived Class 2
Salary :65000.0
Base Class
Name :Tom
Emp_No :12001
=========================
Derived Class 1
Department Name :IT Division
Base Class
Name :Tom
Emp_No :12001

4. Multiple Inheritance

In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. Java does not support multiple inheritances with classes. We can achieve multiple inheritances only through Interfaces.

5. Hybrid Inheritance

 It is a combination of single and multiple inheritance. As Java doesn’t support multiple inheritances with classes, hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s