java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to 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 com.javacoderanch.example.fundamental;

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

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

    public String getType() {
        return type;

    public String getBrand() {
        return brand;

    public String getModel() {
        return model;

    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 com.javacoderanch.example.fundamental;

public class Truck extends Car {
    private int loadCapacity;

    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;

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