Stackify is now BMC. Read theBlog

OOP Concept for Beginners: What is Inheritance?

By: Thorben
  |  March 18, 2023
OOP Concept for Beginners: What is Inheritance?

Inheritance is one of the core concepts of object-oriented programming (OOP) languages. It is a mechanism where you can to derive a class from another class for a hierarchy of classes that share a set of attributes and methods.

You can use it to declare different kinds of exceptions, add custom logic to existing frameworks, and even map your domain model to a database.

Try Stackify’s free code profiler, Prefix, to write better code on your workstation. Prefix works with .NET, Java, PHP, Node.js, Ruby, and Python.

Declare an inheritance hierarchy

In Java, each class can only be derived from one other class. That class is called a superclass, or parent class. The derived class is called subclass, or child class.

You use the keyword extends to identify the class that your subclass extends. If you don’t declare a superclass, your class implicitly extends the class Object. Object is the root of all inheritance hierarchies; it’s the only class in Java that doesn’t extend another class.

The following diagram and code snippets show an example of a simple inheritance hierarchy.

The class BasicCoffeeMachine doesn’t declare a superclass and implicitly extends the class Object. You can clone the CoffeeMachine example project on GitHub.

package org.thoughts.on.java.coffee;
import java.util.HashMap;
import java.util.Map; 
 
public class BasicCoffeeMachine { 
    protected Map configMap; 
    protected Map beans; 
    protected Grinder grinder; 
    protected BrewingUnit brewingUnit; 
 
    public BasicCoffeeMachine(Map beans) { 
        this.beans = beans; 
        this.grinder = new Grinder(); 
        this.brewingUnit = new BrewingUnit(); 
 
        this.configMap = new HashMap(); 
        this.configMap.put(CoffeeSelection.FILTER_COFFEE, new Configuration(30, 480)); 
    } 
 
    public Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException { 
        switch (selection) { 
            case FILTER_COFFEE: 
                return brewFilterCoffee(); 
            default: 
                throw new CoffeeException("CoffeeSelection [" + selection + "] not supported!"); 
        } 
    } 
 
    private Coffee brewFilterCoffee() { 
        Configuration config = configMap.get(CoffeeSelection.FILTER_COFFEE); 
 
        // grind the coffee beans 
        GroundCoffee groundCoffee = this.grinder.grind(
            this.beans.get(CoffeeSelection.FILTER_COFFEE), config.getQuantityCoffee()); 
 
        // brew a filter coffee 
        return this.brewingUnit.brew(
            CoffeeSelection.FILTER_COFFEE, groundCoffee, config.getQuantityWater()); 
    } 
 
    public final void addBeans(CoffeeSelection sel, CoffeeBean newBeans)
        throws CoffeeException {
        CoffeeBean existingBeans = this.beans.get(sel);
        if (existingBeans != null) { 
            if (existingBeans.getName().equals(newBeans.getName())) { 
                existingBeans.setQuantity(existingBeans.getQuantity() + newBeans.getQuantity()); 
            } else { 
                throw new CoffeeException(
                    "Only one kind of beans supported for each CoffeeSelection."); 
            } 
        } else { 
            this.beans.put(sel, newBeans); 
        } 
    } 
}

The class PremiumCoffeeMachine is a subclass of the BasicCoffeeMachine class.

package org.thoughts.on.java.coffee; 
import java.util.Map; 
 
public class PremiumCoffeeMachine extends BasicCoffeeMachine { 
    public PremiumCoffeeMachine(Map beans) { 
        // call constructor in superclass 
        super(beans); 
 
       // add configuration to brew espresso 
         this.configMap.put(CoffeeSelection.ESPRESSO, new Configuration(8, 28)); 
    }  
 
    private Coffee brewEspresso() { 
        Configuration config = configMap.get(CoffeeSelection.ESPRESSO); 
 
        // grind the coffee beans 
        GroundCoffee groundCoffee = this.grinder.grind(
            this.beans.get(CoffeeSelection.ESPRESSO), config.getQuantityCoffee()); 
 
        // brew an espresso 
        return this.brewingUnit.brew(
            CoffeeSelection.ESPRESSO, groundCoffee, config.getQuantityWater()); 
    } 
 
    public Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException { 
        if (selection == CoffeeSelection.ESPRESSO) {
            return brewEspresso(); 
        } else {
            return super.brewCoffee(selection);
        } 
    } 
}

Inheritance and access modifiers

Access modifiers define what classes can access an attribute or method. In one of my previous posts on encapsulation, I showed you how you could use them to implement an information-hiding mechanism. But that’s not the only case where you need to be familiar with the different modifiers. They also affect the entities and attributes that you can access within an inheritance hierarchy.

Here’s is a quick overview of the different modifiers:

  • Private attributes or methods can only be accessed within the same class.
  • Attributes and methods without an access modifier can be accessed within the same class, and by all other classes within the same package.
  • Protected attributes or methods can be accessed within the same class, by all classes within the same package, and by all subclasses.
  • Public attributes and methods can be accessed by all classes.

As you can see in that list, a subclass can access all protected and public attributes and methods of the superclass. If the subclass and superclass belong to the same package, the subclass can also access all package-private attributes and methods of the superclass.

I do that twice in the constructor of the PremiumCoffeeMachine class.

public PremiumCoffeeMachine(Map beans) { 
    // call constructor in superclass 
        super(beans); 
 
    // add configuration to brew espresso 
    this.configMap.put(CoffeeSelection.ESPRESSO, new Configuration(8, 28)); 
}

I first use the keyword super to call the constructor of the superclass. The constructor is public, and the subclass can access it. The keyword super references the superclass. You can use it to access an attribute, or to call a method of the superclass that gets overridden by the current subclass. But more about that in the following section.

The protected attribute configMap gets defined by the BasicCoffeeMachine class. By extending that class, the attribute also becomes part of the PremiumCoffeeMachine class, and I can add the configuration that’s required to brew an espresso to the Map.

Method overriding

Inheritance not only adds all public and protected methods of the superclass to your subclass, but it also allows you to replace their implementation. The method of the subclass then overrides the one of the superclass. That mechanism is called polymorphism.

I use that in the PremiumCoffeeMachine class to extend the coffee brewing capabilities of the coffee machine. The brewCoffee method of the BasicCoffeeMachine method can only brew filter coffee.

public Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException { 
    switch (selection) { 
        case FILTER_COFFEE: 
            return brewFilterCoffee(); 
        default: 
            throw new CoffeeException("CoffeeSelection [" + selection + "] not supported!"); 
    } 
}

I override that method in the PremiumCoffeeMachine class to add support for the CoffeeSelection.ESPRESSO. As you can see in the code snippet, the super keyword is very helpful if you override a method. The brewCoffee method of the BasicCoffeeMachine already handles the CoffeeSelection.FILTER_COFFEE and throws a CoffeeException for unsupported CoffeeSelections.

I can reuse that in my new brewCoffee method. Instead of reimplementing the same logic, I just check if the CoffeeSelection is ESPRESSO. If that’s not the case, I use the super keyword to call the brewCoffee method of the superclass.

public Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException { 
    if (selection == CoffeeSelection.ESPRESSO) {
        return brewEspresso(); 
    } else {
       return super.brewCoffee(selection);
    } 
}

Prevent a method from being overridden

If you want to make sure that no subclass can change the implementation of a method, you can declare it to be final. In this post’s example, I did that for the addBeans method of the BasicCoffeeMachine class.

public final void addBeans(CoffeeSelection sel, CoffeeBean newBeans)
    throws CoffeeException { 
    CoffeeBean existingBeans = this.beans.get(sel);
    if (existingBeans != null) { 
        if (existingBeans.getName().equals(newBeans.getName())) { 
            existingBeans.setQuantity(
                existingBeans.getQuantity() + newBeans.getQuantity()); 
        } else { 
            throw new CoffeeException(
                "Only one kind of beans supported for each CoffeeSelection."); 
        } 
    } else { 
        this.beans.put(sel, newBeans); 
    } 
}

It’s often a good idea to make all methods final that are called by a constructor. It prevents any subclass from, often unintentionally, changing the behavior of the constructor.

A subclass is also of the type of its superclass

A subclass not only inherits the attributes and methods of the superclass, but it also inherits the types of the superclass. In the example, the BasicCoffeeMachine is of type BasicCoffeeMachine and Object. And a PremiumCoffeeMachine object is of the types PremiumCoffeeMachine, BasicCoffeeMachine, and Object.

Due to this, you can cast a PremiumCoffeeMachine object to type BasicCoffeeMachine.

BasicCoffeeMachinee coffeeMachine =
    (BasicCoffeeMachine) PremiumCoffeeMachine(beans);

That enables you to write code that uses the superclass and execute it with all subclasses.

public void makeCoffee() throws CoffeeException { 
    BasicCoffeeMachine coffeeMachine = createCoffeeMachine(); 
    coffeeMachine.brewCoffee(CoffeeSelection.ESPRESSO); 
} 
 
private BasicCoffeeMachine createCoffeeMachine() { 
    // create a Map of available coffee beans 
    Map<CoffeeSelection, CoffeeBean> beans =
        new HashMap<CoffeeSelection, CoffeeBean>();
    beans.put(CoffeeSelection.ESPRESSO,
        new CoffeeBean("My favorite espresso bean", 1000));
    beans.put(CoffeeSelection.FILTER_COFFEE,
        new CoffeeBean("My favorite filter coffee bean", 1000)); 
 
    // instantiate a new CoffeeMachine object 
    return new PremiumCoffeeMachine(beans); 
}

In this example, the code of the createCoffeeMachine method returns and the makeCoffee method uses the BasicCoffeeMachine class. But the createCoffeeMachine method instantiates a new PremiumCoffeeMachine object. When it gets returned by the method, the object is automatically cast to BasicCoffeeMachine and the code can call all public methods of the BasicCoffeeMachine class.

The coffeeMachine object gets cast to BasicCoffeeMachine, but it’s still a PremiumCoffeeMachine. So when the makeCoffee method calls the brewCoffee method, it calls the overridden method on the PremiumCoffeeMachine class.

Defining abstract classes

Abstract classes are different than the other classes that we’ve talked about. They can be extended, but not instantiated. That makes them ideal to represent conceptual generalizations that don’t exist in your specific domain, but enable you to reuse parts of your code.

You use the keyword abstract to declare a class or method to be abstract. An abstract class doesn’t need to contain any abstract methods. But an abstract method needs to be declared by an abstract class.

Let’s refactor the coffee machine example and introduce the AbstractCoffeeMachine class as the superclass of the BasicCoffeeMachine class. I declare that class as abstract and define the abstract brewCoffee method.

public abstract class AbstractCoffeeMachine { 
    protected Map<CoffeeSelection, Configuration> configMap; 
 
    public AbstractCoffeeMachine() { 
        this.configMap = new HashMap<CoffeeSelection, Configuration>(); 
    } 
 
    public abstract Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException; 
}

As you can see, I don’t provide the body of the abstract brewCoffee method. I just declare it as I would do in an interface. When you extend the AbstractCoffeeMachine class, you will need to define the subclass as abstract, or override the brewCoffee method to implement the method body.

I do some minor changes to the BasicCoffeeMachine class. It now extends the AbstractCoffeeMachine class, and the already existing brewCoffee method overrides the abstract method of the superclass.

public class BasicCoffeeMachine extends AbstractCoffeeMachine { 
 
    public BasicCoffeeMachine(Map<CoffeeSelection, CoffeeBean> beans) { 
        super(); 
        this.beans = beans; 
        this.grinder = new Grinder(); 
        this.brewingUnit = new BrewingUnit(); 
 
        this.configMap.put(
           CoffeeSelection.FILTER_COFFEE, new Configuration(30, 480)); 
    } 
 
    public Coffee brewCoffee(CoffeeSelection selection) throws CoffeeException { 
        switch (selection) { 
            case FILTER_COFFEE: 
                return brewFilterCoffee(); 
            default: 
                throw new CoffeeException("CoffeeSelection [" + selection + "] not supported!"); 
        } 
    } 
 
    // .... 
}

Another thing I changed is the constructor of the BasicCoffeeMachine class. It now calls the constructor of the superclass and adds a key-value pair to the configMap attribute without instantiating the Map. It is defined and instantiated by the abstract superclass and can be used in all subclasses.

This is one of the main differences between an abstract superclass and an interface. The abstract class not only allows you to declare methods, but you can also define attributes that are not static and final.

Summary

As you’ve seen, inheritance is a powerful concept that enables you to implement a subclass that extends a superclass. By doing that, the subclass inherits all protected and public attributes and methods, and the types of the superclass. You can then use the inherited attributes of the superclass, use or override the inherited methods, and cast the subclass to any type of its superclass.

You can use an abstract class to define a general abstraction that can’t be instantiated. Within that class, you can declare abstract methods that need to be overridden by non-abstract subclasses. That is often used if the implementation of that method is specific for each subclass, but you want to define a general API for all classes of the hierarchy.

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More

Want to contribute to the Stackify blog?

If you would like to be a guest contributor to the Stackify blog please reach out to stackify@stackify.com