Java Inheritance Tutorial

In this Java tutorial we learn how to inherit functionality from a class, and reuse it in another without having to rewrite the code.

We discuss how to create child classes, as well as inheriting constructor functionality.

Here's a table of contents of what you'll learn in this lesson:
(click on a link to skip to its section)

Let's jump right in.

What is class inheritance

Inheritance makes it possible to reuse code from other classes.

A developer can wrap common sections of code in one class, then inherit the functionality in another and reuse that code in a sub, or child class, that can extend the functionality.

The main class is known as the base, parent or super class. The class that inherits code from it is known as the sub, or child class.

Inheritance is referred to as an is-a type relationship between classes. For example, a cat is an animal, or a car is a vehicle.

Inheritance also provides us with polymorphic behavior, which can be quite powerful.

How to create a sub (child) class in Java

To create a class that inherits from another, we use the extends keyword.

Syntax:
class parent_class {

}

class child_class extends parent_class {

}

When we inherit from a parent class, the child class will receive all the properties and methods that the parent class has.

Example:
public class Program {
    public static void main(String[] args) {

        Dog goofy = new Dog();
        Duck donald = new Duck();

        // both child classes have access
        // to the 'move' method in the
        // parent class
        goofy.move();
        donald.move();

        // as well as their own functionality
        goofy.growl();
        donald.quack();
    }
}

// parent class
class Animal {

    public void move() {
        System.out.println("Animal parent class. Move...");
    }
}

// child class
class Dog extends Animal {

    public void growl() {
        System.out.println("Dog child class. A dog can growl, but a duck can't. Grrr...");
    }
}

// child class
class Duck extends Animal {

    public void quack() {
        System.out.println("Duck Child class. A duck can quack, but a dog can't. Quack...");
    }
}

In the example above, we create two child classes, ‘Dog’ and ‘Duck’, that inherit from the parent class ‘Animal’.

All types of animals can move in some capacity, so it’s included in the parent class. Any child class that inherits from animal, will have the move functionality available to it.

Child classes have more specific functionality that won’t make sense in the parent class. An example would be growling, because not all animals can growl.

As another example, consider an online forum. Users of the forum typically have different roles, such as member, moderator and administrator.

  • A member, moderator and administrator may create posts.
  • A moderator and administrator may move posts to other categories, but a member can’t.
  • An administrator may create categories, but a moderator and member can’t.

So, the parent class could include the functionality to create posts, so as to be available to all roles. Child classes would be used to add functionality for different roles.

Inherit constructor functionality with super in Java

If a child class doesn’t have its own constructor, we can use the constructor of the parent class.

To do this we define a child constructor method and call the super() method from inside its body.

Example:
public class Program {
    public static void main(String[] args) {

        // will use 'Animal' class
        // constructor and initialize
        // the 'name' property as "Unknown"
        Dog dog1 = new Dog();

        System.out.println(dog1.name);
    }
}

// parent class
class Animal {

    String name;

    // constructor
    Animal() {
        this.name = "Unknown";
    }
}

// child class
class Dog extends Animal {

    // empty child
    // constructor
    Dog() {

        // call 'super' to use
        // parent class constructor
        super();
    }
}

In the example above, our child constructor class is empty except for the ‘super’ method.

The super method will use the parent class constructor to initialize the ‘name’ parameter. When we run the script, we see that the parent constructor was successfully used.

Output:
 Unknown

When a parent constructor has parameters, we need to pass them through the child constructor, into the super method.

Example:
public class Program {
    public static void main(String[] args) {

        // the constructor now
        // requires us to pass
        // an argument
        Dog dog1 = new Dog("Goofy");

        System.out.println(dog1.name);
    }
}

// parent class
class Animal {

    String name;

    // constructor
    Animal(String name) {
        this.name = name;
    }
}

// child class
class Dog extends Animal {

    // parent constructor has
    // 'name' param so we need
    // to pass it through here
    // to the 'super' method
    Dog(String name) {

        // 'super' is a normal method
        // call so it takes 'name' as
        // an argument
        super(name);
    }
}

In the example above, we gave the parent constructor a parameter. We need to take the same parameter in the child constructor, so that we can pass it to the super method as an argument.

Summary: Points to remember

  • Inheritance allows one class to inherit functionality from another without rewriting the same code.
  • We use the extends keyword to indicate that one class inherits from another.
  • The child class will automatically receive all the functionality from the parent class unless it contains its own constructor.
  • We inherit constructor functionality with the super() constructor.
    • If the parent class constructor has any parameters, the super() constructor will need the same parameters.