The perfect place for easy learning...

Java Programming

×

Topics List


Java Inheritance Basics





Inheritance Concept

The inheritance is a very useful and powerful concept of object-oriented programming. In java, using the inheritance concept, we can use the existing features of one class in another class. The inheritance provides a greate advantage called code re-usability. With the help of code re-usability, the commonly used code in an application need not be written again and again.

Inheritance Types | Java Inheritance Types

The inheritance can be defined as follows.

The inheritance is the process of acquiring the properties of one class to another class.

Inheritance Basics

In inheritance, we use the terms like parent class, child class, base class, derived class, superclass, and subclass.

The Parent class is the class which provides features to another class. The parent class is also known as Base class or Superclass.

The Child class is the class which receives features from another class. The child class is also known as the Derived Class or Subclass.

In the inheritance, the child class acquires the features from its parent class. But the parent class never acquires the features from its child class.


There are five types of inheritances, and they are as follows.

  • Simple Inheritance (or) Single Inheritance
  • Multiple Inheritance
  • Multi-Level Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

The following picture illustrates how various inheritances are implemented.

Inheritance Types | Java Inheritance Types

The java programming language does not support multiple inheritance type. However, it provides an alternate with the concept of interfaces.


Creating Child Class in java

In java, we use the keyword extends to create a child class. The following syntax used to create a child class in java.

Syntax
class <ChildClassName> extends <ParentClassName>{
    ...
    //Implementation of child class
    ...
}

In a java programming language, a class extends only one class. Extending multiple classes is not allowed in java.


Let's look at individual inheritance types and how they get implemented in java with an example.

Single Inheritance in java

In this type of inheritance, one child class derives from one parent class. Look at the following example code.

Example
class ParentClass{
	int a;
	void setData(int a) {
		this.a = a;
	}
}
class ChildClass extends ParentClass{
	void showData() {
		System.out.println("Value of a is " + a);
	}
}
public class SingleInheritance {

	public static void main(String[] args) {

		ChildClass obj = new ChildClass();
		obj.setData(100);
		obj.showData();

	}

}

When we run this code, it produce the following output.

inheritance in java

Multi-level Inheritance in java

In this type of inheritance, the child class derives from a class which already derived from another class. Look at the following example java code.

Example
class ParentClass{
	int a;
	void setData(int a) {
		this.a = a;
	}
}
class ChildClass extends ParentClass{
	void showData() {
		System.out.println("Value of a is " + a);
	}
}
class ChildChildClass extends ChildClass{
	void display() {
		System.out.println("Inside ChildChildClass!");
	}
}
public class MultipleInheritance {

	public static void main(String[] args) {

		ChildChildClass obj = new ChildChildClass();
		obj.setData(100);
		obj.showData();
		obj.display();

	}

}

When we run this code, it produce the following output.

inheritance in java

Hierarchical Inheritance in java

In this type of inheritance, two or more child classes derive from one parent class. Look at the following example java code.

Example
class ParentClass{
	int a;
	void setData(int a) {
		this.a = a;
	}
}
class ChildClass extends ParentClass{
	void showData() {
		System.out.println("Inside ChildClass!");
		System.out.println("Value of a is " + a);
	}
}
class ChildClassToo extends ParentClass{
	void display() {
		System.out.println("Inside ChildClassToo!");
		System.out.println("Value of a is " + a);
	}
}
public class HierarchicalInheritance {

	public static void main(String[] args) {

		ChildClass child_obj = new ChildClass();
		child_obj.setData(100);
		child_obj.showData();
        
		ChildClassToo childToo_obj = new ChildClassToo();
		childToo_obj.setData(200);
		childToo_obj.display();

	}

}

When we run this code, it produce the following output.

inheritance in java

Hybrid Inheritance in java

The hybrid inheritance is the combination of more than one type of inheritance. We may use any combination as a single with multiple inheritances, multi-level with multiple inheritances, etc.,