How do I override a method in Java?

If a subclass has a method that have the same signature with a method in its superclass, then it’s an overriding method. Override inherited methods allow subclasses to provide specialized implementation for those methods. The overriding method has the same name, number and type of arguments, and return value as the method it overrides.

The overriding method can have a different throws clause as long as it doesn’t specify any types not specified by the throws clause in the overridden method. Also, the access specifier for the overriding method can allow more but not less access than the overridden method.

For example, a protected method in the superclass can be made public but not private. A subclass cannot override methods that are declared final in the superclass. A subclass must override methods that are declared abstract in the superclass, or the subclass itself must be abstract.

package org.kodejava.example.fundamental;

public class Car {
    private String type;
    private String brand;
    private String model;
    private int numberOfSeat;

    public Car() {
    }

    public Car(String type, String brand, String model) {
        this.type = type;
        this.brand = brand;
        this.model = model;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getNumberOfSeat() {
        return numberOfSeat;
    }

    public void setNumberOfSeat(int numberOfSeat) {
        this.numberOfSeat = numberOfSeat;
    }

    public String getCarInfo() {
        return"Type: " + type
                + "; Brand: " + brand
                + "; Model: " + model;
    }
}

In the Truck class we override the getCarInfo() method to provide more information about the truck object. We can also add an @Override annotation to an overriding method.

package org.kodejava.example.fundamental;

public class Truck extends Car {
    private int loadCapacity;

    public Truck() {
    }

    public Truck(String type, String brand, String model) {
        super(type, brand, model);
    }

    public int getLoadCapacity() {
        return loadCapacity;
    }

    public void setLoadCapacity(int loadCapacity) {
        this.loadCapacity = loadCapacity;
    }

    @Override
    public String getCarInfo() {
        return "Type: " + getType()
                + "; Brand: " + getBrand()
                + "; Model: " + getModel()
                + "; Load capacity: " + getLoadCapacity();
    }
}

How do I use @Override annotation?

We use the @Override annotation as part of method declaration. The @Override annotation is used when we want to override methods and want to make sure have overridden the correct methods.

As the annotation name we know that there should be the same method signature in the parent class to override. That means using this annotation let us know earlier when we are mistakenly override method that doesn’t exist in the base class.

package org.kodejava.basic;

public class OverrideExample {
    private String field;
    private String attribute;

    @Override
    public int hashCode() {
        return field.hashCode() + attribute.hashCode();
    }

    @Override
    public String toString() {
        return field + " " + attribute;
    }
}