Java: Inheritance, Superclass and Subclass

Coworkers discussing data on computer
AMV Photo/Digital Vision/Getty Images

An important concept in object-oriented programming is inheritance. It provides a way for objects to define relationships with each other. As the name suggests, an object is able to inherit characteristics from another object.

In more concrete terms, an object is able to pass on its state and behaviors to its children. For inheritance to work, the objects need to have characteristics in common with each other.

In Java, classes can be taken from other classes, which can be taken from others, and so on. This is because they can inherit features from the class above it, all the way up to the topmost Object class.

An Example of Java Inheritance

Let's say we make a class called Human that represents our physical characteristics. It's a generic class that could represent you, me, or anyone in the world. Its state keeps track of things like number of legs, number of arms, and blood type. It has behaviors like eat, sleep, and walk.

Human is good for getting an overall sense of what makes us all the same but it can't, for instance, tell me about gender differences. For that, we'd need to make two new class types called Man and Woman. The state and behaviors of these two classes will differ from each other in a lot of ways except for the ones that they inherit from Human.

Therefore, inheritance allows us to encompass the parent class' state and behaviors into its child.

The child class can then extend the state and behaviors to reflect the differences it represents. The most important aspect of this concept to remember is that the child class is a more specialized version of the parent.

What's a Superclass?

In the relationship between two objects, a superclass is the name given to the class that is being inherited from.

It sounds like a super duper class, but remember that it's the more generic version. Better names to use might be base class or simply parent class.

To take a more real world example this time, we could have a superclass called Person. Its state holds the person's name, address, height, and weight, and has behaviors like go shopping, make the bed, and watch TV.

We could make two new classes that inherit from Person called Student and Worker. They are more specialized versions because although they have names, addresses, watch TV, and go shopping, they also have characteristics that are different from each other.

Worker could have a state that holds a job title and place of employment whereas Student might hold data on an area of study and an institution of learning.

Superclass Example:

Imagine you define a Person class:

 public class Person
 {
 
 } 

A new class can be created by extending this class:

 public class Employee extends Person
 {
 
 } 

The Person class is said to be the superclass of the Employee class.

What's a Subclass?

In the relationship between two objects, a subclass is the name given to the class that is inheriting from the superclass. Although it sounds a little more drab, remember that it's a more specialized version of the superclass.

In the previous example, Student and Worker are the subclasses.

Subclasses can also be known as derived classes, child classes, or extended classes.

How Many Subclasses Can I Have?

You can have as many subclasses as you want. There is no limitation to how many subclasses a superclass can have. Likewise, there isn't a limitation on the number of levels of inheritance. A hierarchy of classes can be built upon a certain area of commonality.

In fact, if you look at the Java API libraries you will see many examples of inheritance. Every class in the APIs is inherited from a class called java.lang.Object. For example, any time you use a JFrame object, you're at the end of a long line of inheritance:

 java.lang.Object
 extended by java.awt.Component
 extended by java.awt.Container
 extended by java.awt.Window
 extended by java.awt.Frame
 extended by javax.swing.JFrame

In Java, when a subclass inherits from a superclass, it's known as "extending" the superclass.

Can My Subclass Inherit From Many Superclasses?

No. In Java, a subclass can only extend one superclass.

Why Use Inheritance?

Inheritance allows programmers to re-use code they've already written. In the Human class example, we don't need to create new fields in the Man and Woman class to hold the blood type because we can use the one inherited from the Human class.

Another benefit of using inheritance is that it lets us treat a subclass as if it was a superclass. For example, let's say a program has created multiple instances of the Man and Woman objects. The program might need to call the sleep behavior for all these objects. Because the sleep behavior is a behavior of the Human superclass, we can group all the Man and Woman objects together and treat them as if they were Human objects.