Java Method Overriding, @Override and the Object Class
Like a restaurant branch that keeps, adapts or replaces the head-office recipe: Manager adapts work() with super.work(), Developer replaces it, Director keeps Manager's. Then overload vs override, the rules, and a Badge overriding Object's toString and equals.
What you will learn
- Override a parent method to adapt it with super.work() or replace it
- Explain how a call climbs the class chain to the first matching method
- Tell overriding from overloading and follow the overriding rules
- Override toString, equals and hashCode inherited from Object
Before you start
- Java inheritance: extends and super
- Java methods and overloading
Think of a restaurant chain. Head office has a recipe, and each branch can keep it, adapt it or replace it. Overriding is the same idea for methods: a child class writes a method with the same name and parameters as its parent's, with its own body.
The examples build on the Employee family from inheritance. In Employee, work() returns name + " is working".
Adapt the recipe, or replace it
package org.studyeasy.s03l11;
// A Manager adapts the recipe: same name, same parameters, its own body
public class Manager extends Employee {
protected int teamSize;
public Manager(String name, double salary, int teamSize) {
super(name, salary);
this.teamSize = teamSize;
}
@Override
public String work() {
// reuse the parent's version, then add the team
return super.work() + " and leads " + teamSize + " people";
}
}
package org.studyeasy.s03l11;
// A Developer replaces the recipe completely
public class Developer extends Employee {
private String language;
public Developer(String name, double salary, String language) {
super(name, salary);
this.language = language;
}
@Override
public String work() {
return name + " is writing " + language;
}
}
@Override asks the compiler to check that the method really matches one in the parent. Manager calls super.work(), so the parent's version runs first and Manager adds to it. Developer has no super call, so Employee's text isn't used at all.
Employee arun = new Employee("Arun", 60000);
Manager emma = new Manager("Emma", 95000, 6);
Developer lucas = new Developer("Lucas", 80000, "Java");
System.out.println(arun.work()); // Employee's version
System.out.println(emma.work()); // Manager's version
System.out.println(lucas.work()); // Developer's version
Arun is working
Emma is working and leads 6 people
Lucas is writing Java
Same method name, three objects, three results.
The call climbs to the first work()
Director extends Manager and writes no work(); it keeps the recipe it was given.
Director priya = new Director("Priya", 150000, 40, "Sales");
// Director has no work(): Java climbs to Manager and stops there
System.out.println(priya.work());
System.out.println(priya.approveBudget());
Priya is working and leads 40 people
Priya approves the Sales budget
Override or overload?
Employee also has work(String task), returning name + " is working on " + task. Same name, different parameters: that's an overload, a second method.
Manager emma = new Manager("Emma", 95000, 6);
System.out.println(emma.work()); // override: Manager's own body
System.out.println(emma.work("the budget")); // overload: Employee's second method
Emma is working and leads 6 people
Emma is working on the budget
| Override | Overload | |
|---|---|---|
| Name | same | same |
| Parameters | same | different |
| Where | in a child class | same class or a child |
| Which runs | the object's own version | the one that fits the arguments |
@Override | yes | no |
Overriding replaces a method; overloading adds another one.
The rules for overriding
Break any of these and the compiler tells you straight away (the project's BREAK IT notes show each message):
- Name and parameters match exactly. Rename
work()towrok()with@Overrideon it: "method does not override or implement a method from a supertype". - Visibility stays the same or gets wider. Drop
publicin Developer: "attempting to assign weaker access privileges; was public". - The return type is compatible. Make Manager's
work()returnintand it can't override a String method. - A final method can't be overridden. Make Employee's
work()final: "overridden method is final". - Always add
@Override, so typos are caught.
Every class extends Object
A class that writes no extends, like Employee, still has a parent: Java adds extends Object for you. So every class inherits methods like toString, equals and getClass, and you can override them just like work().
package org.studyeasy.s03l11;
// No extends written, yet Badge extends Object, so it can override Object's methods
public class Badge {
private int id;
private String owner;
public Badge(int id, String owner) {
this.id = id;
this.owner = owner;
}
@Override
public String toString() {
return "Badge #" + id + " (" + owner + ")";
}
@Override
public boolean equals(Object other) {
if (other == null || getClass() != other.getClass()) {
return false; // not a Badge at all
}
Badge that = (Badge) other;
return id == that.id; // same id means the same badge
}
@Override
public int hashCode() {
return id; // equal badges must give the same number
}
}
Badge first = new Badge(101, "Emma");
Badge copy = new Badge(101, "Emma");
System.out.println(first); // calls toString()
System.out.println("== " + (first == copy));
System.out.println("equals " + first.equals(copy));
System.out.println("class " + first.getClass().getSimpleName());
Badge #101 (Emma)
== false
equals true
class Badge
== is false: two objects. Our equals compares ids, so it's true. getClass() is inherited from Object unchanged. Note the parameter type: equals(Object other). Write equals(Badge other) and it no longer overrides Object's method; @Override catches that. Why hashCode must go with equals is explained in equals and hashCode.
Recap
- Override: same name and parameters in a child, its own body, marked
@Override. super.work()reuses the parent's version inside an override.- Java runs the first matching method it finds, climbing from the object's own class.
- Every class extends
Object; overridetoStringandequalsto make them useful.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java Classes and Objects: One Blueprint, Many Cars
Six loose variables for two cars doesn't scale. A Car class is the blueprint; new builds each car with its own brand, speed and fuel. Plus field defaults, and why two variables can point at one car.
Java Constructors and the this Keyword
A constructor decides how an object is born: new Car("Toyota", 42.5) arrives filled in. this.brand vs brand, a second constructor that calls this(brand, 50.0), and why new Car() stops compiling once you write your own.
Java Instance Methods, Getters, Setters and toString
Give a Car class behaviour: accelerate() changes only this car's speed, private fields are reached through getSpeed and setSpeed, and toString turns Car@1b6d3586 into 'Toyota doing 60 km/h'.
Java static: Fields, Methods and Blocks Shared by the Whole Class
A static field lives once on the class: every Car ticks the same carsBuilt counter while keeping its own brand. Static methods need no object, a static block runs before main, and main is static so the JVM can start with no object.
