Method Overriding in Java

Naveen AnamaneniUpdated · 4 min read

Every employee gets a monthly salary, but a manager also gets an allowance. The Employee class already knows how to work out pay; a Manager needs the same method with a different answer. Method overriding means a subclass provides its own version of an inherited method, with the same name and parameters. When the method is called on a manager, the manager's version runs, even through an Employee variable.

Replacing an Inherited Method

Payroll.java▶ Try it
public class Payroll {
    static class Employee {
        String name;
        long base;
 
        Employee(String name, long base) {
            this.name = name;
            this.base = base;
        }
 
        long monthlyPay() { return base; }
    }
 
    static class Manager extends Employee {
        long allowance;
 
        Manager(String name, long base, long allowance) {
            super(name, base);
            this.allowance = allowance;
        }
 
        @Override
        long monthlyPay() { return super.monthlyPay() + allowance; }
    }
 
    public static void main(String[] args) {
        Employee[] staff = {
            new Employee("Srihitha", 60000),
            new Manager("Navika", 90000, 15000)
        };
        for (Employee e : staff) {
            System.out.println(e.name + ": Rs. " + e.monthlyPay());
        }
    }
}
Output
Srihitha: Rs. 60000
Navika: Rs. 105000

How it works

  1. Manager declares monthlyPay() with exactly the same signature as Employee's. That overrides it.
  2. super.monthlyPay() runs the parent's version (the base pay), and the manager adds the allowance: 90,000 + 15,000.
  3. The loop variable e has type Employee, yet for Navika the Manager version runs. The object's class decides, at runtime.

Try it yourself: add Intern extends Employee whose pay is half the base. Add an intern named Advitha to the array.

Always Write @Override

@Override is an annotation that tells the compiler, "this method must override something". It changes nothing at runtime, but it catches a nasty kind of bug. Here the method name has a lowercase p:

NoAnno.java▶ Try it
public class NoAnno {
    static class Employee { long monthlyPay() { return 50000; } }
    static class Manager extends Employee {
        long monthlypay() { return 90000; }
    }
    public static void main(String[] args) {
        Employee e = new Manager();
        System.out.println(e.monthlyPay());
    }
}
Output
50000

Without @Override, monthlypay() is just a new method, and the manager silently gets the employee's pay. Add @Override and the mistake becomes a compile error:

Compiler output
Typo.java:3: error: method does not override or implement a method from a supertype
    @Override
    ^
1 error

Extend or Replace

An overriding method has two choices:

StyleCodeWhen
Replace the parent's behaviorreturn "SMS to " + to;The child does the job completely differently
Extend the parent's behaviorreturn super.monthlyPay() + allowance;The child does what the parent does, plus more

Extending with super.method() keeps the shared logic in one place: if the base-pay rule changes in Employee, managers pick it up automatically.

Common Mistakes

Changing the parameter list. monthlyPay(int month) in the child is an overload, not an override. The old method is still inherited unchanged.

Leaving out @Override. Typos and wrong parameter types then create new methods instead of errors.

Calling monthlyPay() instead of super.monthlyPay() inside the override. It calls itself forever and ends in a StackOverflowError.

Interview Corner

What is method overriding?

Overriding is when a subclass declares a method with the same name and parameter list as an inherited instance method, to provide its own behavior. At runtime the JVM calls the version belonging to the object's actual class, even through a parent-type reference. It's the basis of runtime polymorphism. The overriding method can call the parent's version with super.method().

Why use the @Override annotation?

It asks the compiler to check that the method really overrides a method from a superclass or interface. If the name, parameters or parent method are wrong, compilation fails instead of silently creating a new method. It also documents intent for readers.

Practice

  1. Easy: Give Account a method String type() returning "Account", override it in SavingsAccount, and call it through an Account variable.
  2. Harder: Override toString() in a Student class (name and marks), then print a Student directly with System.out.println(student).

Quick check: Method overriding

4 questions
1Which method in Manager overrides long monthlyPay() from Employee?
2Employee e = new Manager(...); e.monthlyPay(); runs:
3What does @Override do?
4How does an override reuse the parent's logic?

Key Takeaways

Was this tutorial helpful?

Lesson 123 of 482