Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Object-Oriented Programming

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Object-Oriented Programming

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
The class chain from top to bottom: Object, Employee, Manager, Director. priya.work() starts at Director, finds no work(), climbs to Manager and runs its work(). Manager's super.work() goes one more level to Employee, which returns "Priya is working"; Manager adds "and leads 40 people". Object: toString, equals... Employee: work() Manager: work() runs Director: no work() 1. start at Director: not here 2. climb: found in Manager 3. super.work() → Employee "Priya is working" "... and leads 40 people"
Java starts at the object's own class and climbs; the first matching method runs.

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
OverrideOverload
Namesamesame
Parameterssamedifferent
Wherein a child classsame class or a child
Which runsthe object's own versionthe one that fits the arguments
@Overrideyesno

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() to wrok() with @Override on it: "method does not override or implement a method from a supertype".
  • Visibility stays the same or gets wider. Drop public in Developer: "attempting to assign weaker access privileges; was public".
  • The return type is compatible. Make Manager's work() return int and 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; override toString and equals to make them useful.

Keep going in Java · 33 of 79

Previous
Java Inheritance: extends, super and the Order Objects Are Built
Next post
Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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.

On this page

  • Adapt the recipe, or replace it
  • The call climbs to the first work()
  • Override or overload?
  • The rules for overriding
  • Every class extends Object
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.