Explore the Concept of Overriding in Java
Method overriding happens when a subclass provides its own implementation of a method that is already defined in its superclass. It enables runtime (dynamic) polymorphism.
Rules for Method Overriding
- The method in the subclass must have the same name, return type, and parameters as in the superclass
- The method must be inherited, not static, final, or private
- Access modifier in the overriding method cannot be more restrictive than the overridden method
Example
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
Animal a = new Dog();
a.sound(); // Output: Dog barks
Overriding vs Overloading
- Overriding happens between a superclass and subclass; overloading happens within the same class
- Overriding is resolved at runtime; overloading is resolved at compile time
- Overriding requires the same method signature; overloading requires a different one
@Override annotation is optional but recommended — it lets the compiler catch mistakes if the method doesn't actually override anything.Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.
How Java Picks the Method at Runtime
When the compiler sees a.sound(), it only checks that the declared type Animal has a sound() method. The actual choice happens later, while the program runs:
- The JVM looks at the real object stored in the variable, here a
Dog. - It searches that object's class for a matching method.
- If none is found, it moves up to the parent class, and so on, until it finds one.
This is called dynamic method dispatch. It is why a list of Animal references can hold dogs, cats and cows, and one loop calling sound() prints a different result for each.
Rules That Are Easy to Miss
Covariant return types
The overriding method may return a subtype of the original return type:
class Animal {
Animal create() { return new Animal(); }
}
class Dog extends Animal {
@Override
Dog create() { return new Dog(); } // allowed: Dog is an Animal
}
Checked exceptions
An override can throw the same checked exceptions, narrower ones or none at all, but never new or broader checked exceptions. Unchecked exceptions are not restricted. Code written against the parent must never be surprised by an exception it was not prepared to handle.
Calling the parent version
Use super when you want to extend behaviour rather than replace it:
class LoggingDog extends Dog {
@Override
void sound() {
System.out.println("Before sound");
super.sound(); // runs Dog's version
}
}
Static Methods and Fields Do Not Override
A static method with the same signature in a child class hides the parent method. The version called depends on the declared type, not the object:
class Parent { static String who() { return "Parent"; } }
class Child extends Parent { static String who() { return "Child"; } }
Parent p = new Child();
System.out.println(p.who()); // Parent
Fields behave the same way. If both classes declare a field named name, the field chosen depends on the reference type. This is one reason to keep fields private and expose them through methods.
Practical Overrides and a Classic Trap
The methods you will override most often come from Object: toString() for readable output, and equals() with hashCode() for comparing objects. Always override equals() and hashCode() together, otherwise objects that are "equal" can still be treated as different keys in a HashMap or HashSet.
A classic trap is calling an overridable method from a constructor:
class Base {
Base() { init(); }
void init() { }
}
class Derived extends Base {
private String name = "ready";
@Override
void init() {
System.out.println(name.length()); // NullPointerException
}
}
The parent constructor runs before the child's fields are initialised, so name is still null when init() executes. Keep constructors simple, or mark such helper methods private or final.
Checklist: add @Override, keep the same parameters, do not reduce visibility, do not add broader checked exceptions, and use super when the parent's work should still happen.