Method Overriding and Hiding
1. Method Overriding
Overriding happens when a subclass provides its own implementation of a parent's instance method with the same signature.
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Woof");
}
}
Animal animal = new Dog();
animal.sound();
Output:
Woof
The actual object (Dog) decides which instance method runs.
@Override
Use @Override to tell Java that you intend to override a method.
@Override
void sound() {
}
If you make a mistake in the method signature, the compiler reports an error.
2. Static Method Hiding
Static methods are not overridden. They are hidden.
class Animal {
static void info() {
System.out.println("Animal");
}
}
class Dog extends Animal {
static void info() {
System.out.println("Dog");
}
}
Now:
Animal animal = new Dog();
animal.info();
Dog.info();
Output:
Animal
Dog
Why?
Static method → reference/class type decides.
Instance method → actual object decides.
3. Instance vs Static
Animal animal = new Dog();
| Method | Which version runs? |
|---|---|
| Instance method | Dog |
| Static method | Animal |
This is one of the most important differences to remember.
4. Interface Default Methods
Interfaces can have default methods.
interface Flyer {
default void move() {
System.out.println("Flying");
}
}
interface Swimmer {
default void move() {
System.out.println("Swimming");
}
}
If a class implements both, there is a conflict:
class Duck implements Flyer, Swimmer {
@Override
public void move() {
Flyer.super.move();
}
}
You can choose a specific interface's implementation:
Flyer.super.move();
Easy rule
Two interfaces provide the same default method → class must resolve the conflict.
5. Class Method Beats Interface Default Method
A class's inherited/declared instance method has priority over an interface's default method.
class Animal {
void sound() {
System.out.println("Animal");
}
}
interface Pet {
default void sound() {
System.out.println("Pet");
}
}
class Dog extends Animal implements Pet {
}
Dog dog = new Dog();
dog.sound();
Output:
Animal
The class method wins over the interface default method.
6. Interface Abstract Method Can Be Implemented by Parent Class
A subclass doesn't need to implement an interface method if its superclass already provides it.
interface Animal {
void sound();
}
class Horse {
public void sound() {
System.out.println("Horse sound");
}
}
class Mustang extends Horse implements Animal {
}
Mustang gets sound() from Horse, so the interface requirement is already satisfied.
7. Static Methods in Interfaces
Static interface methods are not inherited.
interface Animal {
static void info() {
System.out.println("Animal");
}
}
Call it using the interface:
Animal.info();
Not:
Dog.info(); // ERROR
8. Access Modifiers When Overriding
A subclass cannot reduce the visibility of an overridden method.
class Animal {
protected void sound() {
}
}
class Dog extends Animal {
public void sound() { // OK
}
}
But:
class Dog extends Animal {
private void sound() { // ERROR
}
}
Easy rule
Same or wider access is allowed; narrower access is not.
private → cannot override normally
protected → protected / public
public → public only
9. Static ↔ Instance Cannot Be Changed
You cannot turn a parent's instance method into a static method.
class Animal {
void sound() {
}
}
class Dog extends Animal {
static void sound() { // ERROR
}
}
And the opposite is also not allowed:
class Animal {
static void sound() {
}
}
class Dog extends Animal {
void sound() { // ERROR
}
}
10. Overloading Is Different
Overloading means creating another method with different parameters.
class Animal {
void sound() {
}
void sound(String type) {
}
}
This is not overriding.
Same signature → Overriding
Different parameters → Overloading
Quick Revision
| Concept | Meaning |
|---|---|
| Overriding | Subclass changes an instance method |
| Hiding | Subclass defines same static method |
@Override |
Tells compiler you intend to override |
| Instance method | Actual object decides |
| Static method | Reference/class type decides |
| Default method conflict | Class must resolve it |
| Class method vs default method | Class method wins |
| Interface static method | Not inherited |
| Access modifier | Cannot reduce visibility |
| Static ↔ instance | Cannot change |
| Overloading | Same method name, different parameters |
Most important rule
Instance methods are overridden; static methods are hidden.
Parent reference → Child object
Instance method → Child version
Static method → Parent/reference type version