Inheritance

In Object-Oriented Programming, inheritance is the process of creating a new subclass based on an existing base class. The subclass will derive all the base class instance variables and methods.

We can also call the base class as the parent class or super class, and the subclass as child class.

public class SubClass extends BaseClass {
    // Code
}

In Java, we create a new class from base class using extends keyword. In the above code snippet, we can state the relationship by saying that Subclass extends BaseClass or SubClass is derived from BaseClass, or SubClass is inherited from BaseClass.

Benefits of Inheritance

  • Common attributes and behaviors of multiple classes of similar type are combined and defined within base class.
  • Inheritance permits code reusability. If you are planning to enhance a class which is already present, you can do that by inheriting that class and creating your own implementation.
  • Polymorphism is possible only when you have inherited classes from a base class. No inheritance, no polymorphism.

When to use Inheritance?

As explained above, when you have multiple classes of similar type having IS-A relation between them, then it is best to have this organized hierarchical organization of base class and subclasses.

Usually an abstract class will act as the base class. In some use cases, an interface can also be a base interface for the class hierarchy.

Types of Inheritance

There are several inheritance types in object-oriented programming.

Single Inheritance

When a subclass is derived from only 1 parent class, that is called as a single inheritance.

Multiple Inheritance

In multiple inheritance, we can derive a class from more than one base class.

For example, there are two classes called as Architect and Manager. If we create a new class called as Technical Manager deriving from both Architect and Manager, this inheritance will be known as multiple inheritance.

But in Java a class can inherit from only one base class, it cannot have more than one base class. Java provides interfaces to solve the multiple inheritance problem. A class can implement more than one interface.

Multilevel Inheritance

Multilevel Inheritance is a chain of subclasses inheriting one another, beginning with the base class.

Hierarchical Inheritance

In a hierarchical inheritance, a base class has more than one subclass. The relationship of Employee, Manager, Developer, Team Lead is a hierarchical inheritance. Here, Employee is the base class, Manager, Developer and Team Lead are subclasses of Employee.

Hybrid Inheritance

Hybrid Inheritance is a combination of Single/Multilevel Inheritance and Multiple Inheritance. We also refer to Hybrid Inheritance as Diamond Inheritance or Diamond Problem.

For example, a class called as Musician is having Guitarist and Pianist as child classes. There is another class called a Multi-Instrumentalist which is derived from Guitarist and Pianist.

In Java, you can achieve Hybrid Inheritance with the help of interfaces. Musician, Guitarist, and Pianist must be interfaces and Multi-Instrumentalist must be a class.

Inheritance Example

package com.techstackjournal.inheritance.types;

public class Alpha {
	public String alpha1 = "Alpha 1";
	private String alpha2 = "Alpha 2";

	public void displayAlpha() {
		System.out.println(alpha1);
		System.out.println(alpha2);
	}
}
// File Name: Beta.java

package com.techstackjournal.inheritance.types;

public class Beta extends Alpha {
	private String beta = "Beta";

	public void displayBeta() {
		System.out.println(beta);
	}
}

Here, Alpha is the base class, and Beta is the subclass. Since Beta extends Alpha, all public instance variables and methods of Alpha will be visible to Beta. So, here, alpha1 and displayAlpha will be visible in Beta. Since alpha2 is declared as private it will not be accessible within Beta class.

// File Name: Application.java

package com.techstackjournal.inheritance.main;

import com.techstackjournal.inheritance.types.Beta;

public class Application {

	public static void main(String[] args) {

		Beta b = new Beta();
		b.displayAlpha();
		b.displayBeta();

	}

}

In this main program, you can see that we have created an instance of Beta class and are able to access the method of class Alpha also.

Object Class

Each class you create in Java class extends an Object class implicitly. You can check this by hitting dot operator right after the reference variable, you would see a bunch of methods which are not in your class. All these methods are coming from the Object class.

IS-A vs HAS-A

  • Inheritance is used to describe IS-A relationship between subclass and super class.
  • For example, if Circle extends Shape, we call it Circle IS-A Shape. Here Shape will be called as the super class and Circle will be called as subclass.
  • When a class wraps instance variables of other classes in it, it models a HAS-A relationship between them
  • For example, if a Circle contains an instance variable of type Color, we say that both Circle and Color are in HAS-A relation, Circle HAS-A Color.