Method Overriding in Java
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
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());
}
}
}Srihitha: Rs. 60000
Navika: Rs. 105000How it works
ManagerdeclaresmonthlyPay()with exactly the same signature asEmployee's. That overrides it.super.monthlyPay()runs the parent's version (the base pay), and the manager adds the allowance: 90,000 + 15,000.- The loop variable
ehas typeEmployee, yet for Navika theManagerversion 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:
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());
}
}50000Without @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:
Typo.java:3: error: method does not override or implement a method from a supertype
@Override
^
1 errorExtend or Replace
An overriding method has two choices:
| Style | Code | When |
|---|---|---|
| Replace the parent's behavior | return "SMS to " + to; | The child does the job completely differently |
| Extend the parent's behavior | return 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
- Easy: Give
Accounta methodString type()returning"Account", override it inSavingsAccount, and call it through anAccountvariable. - Harder: Override
toString()in aStudentclass (name and marks), then print aStudentdirectly withSystem.out.println(student).
Key Takeaways
Was this tutorial helpful?