Inheritance

Manthan Nikam
4 min readMay 13, 2021

Inheritance is the process of building a new class based on the features of another existing class. It is used heavily in Java, Python, and other object-oriented languages to increase code reusability and simplify program logic into categorical and hierarchical relationships.

However, each language has its own unique way of implementing inheritance that can make switching difficult.

Today, we’ll give you a crash course on the uses of inheritance in Java programming and show you how to implement the core inheritance tools like typecasting, method overriding, and final entities.

Here’s what we’ll cover today:

Adopt Java in half the time

Get hands-on practice with our best Java content tailored to current developer skill levels.

Java for Programmers

What is Inheritance?

Inheritance is a mechanism that allows one class to inherit properties or behaviors from another class. Multiple classes can inherit from the same parent class, forming a tree-like hierarchy structure. Inheriting classes can add features beyond those inherited from the parent class to allow for unique behavior.

Inheritance is essential to advanced Object Oriented Programming (OOP) as it allows you to reuse one class’s features across your program without replicating code.

Inheritance is often used to represent categories (parent classes) and sub-categories (subclasses). The parent class sets the features present in all objects regardless of subcategory, while each subclass represents a smaller, more specific category.

For example, you could create the class Car that specifies wheels = 4 and a subclass Sedan that includes the attribute doors = 4. The flow of inheritance relationships often reflects the logical relationship similar to squares and rectangles; in this case, all sedans are cars, but not all cars are sedans.

Inheritance has three main advantages:

  1. Reusability: Inheritance allows you to reuse the features of an existing class an unlimited number of times across any class that inherits that class. You can keep consistent functionality across all objects of the same type without rewriting the code.
  2. Code Structure: Inheritance provides a clear, drawable logic structure for your program. It allows developers to understand your code as a collection of related but unique categories rather than simply a block of code.
  3. Data Hiding: The base class can be set to keep some data private so that it cannot be altered by the derived class. This is an example of encapsulation, where access to data is restricted to only the classes that need it for their role.

Inheritance in Java

Each programming language has slightly different terminology for inheritance. In Java, the parent class is called the superclass, and the inheritor class is called the subclass. Developers may also call superclasses base or parent classes and subclasses derived or child classes.

Subclasses are linked to superclasses using the extends keyword during their definition. Subclasses can define new local methods or fields to use or can use the super keyword to call inherited methods or the super constructor.

class b {
// implementation of inheritedMethod()
}
class a extends b
{
inheritedMethod();
}

When to use super keyword

super is essentially a “previous value” button called from within a child class that allows you to read and access features from the parent class regardless of their value in the current child class.

The super keyword is used to:

  • Access parent class fields: super.var reads the value of var set in the parent class, while var alone reads the modified value from the child.
  • Calling a parent class method: super.method() allows the child to access the parent class implementation of method(). This is only required if the child class also has a method with the same name.
  • Using Constructors: This allows you to create new instances of the parent class from within a child class.

As a refresher, constructors in Java are special methods used to initialize objects. Calling the super constructor creates a new object that requires all the fields defined in the parent class constructor.

You can then add additional fields in other statements to make the child instance more specific than the parent. Essentially, it allows you to use the parent class constructor as a template for your child class constructor.

Types of Inheritance

There are several types of inheritance available in Java:

  • Single inheritance is when a single subclass inherits from a superclass, forming one layer of inheritance.
  • Multilevel Inheritance is when a superclass is inherited by an intermediate class, which is then inherited by a derived class, forming 3 or more levels of inheritance.
  • Hierarchical inheritance is when one superclass serves as a baseline for multiple specific subclasses. This is the most common form of inheritance.
  • There are also two other types of inheritance that are only available in Java through a combination of class and interface inheritance.
  • Multiple inheritance, when a single subclass inherits from multiple parent classes.
  • Hybrid inheritance, a mix of two or more of the above kinds of inheritance.

Java does not support multiple inheritance with classes, meaning both of these types of inheritance are impossible with Java classes alone. However, a subclass can inherit more than one interface (an abstract class). You can therefore simulate multiple inheritance if you combine the use of interfaces and classes.

--

--