Java Method Overriding
Java method overriding means a subclass provides its own version of a method it inherits from a superclass. The method keeps the same name and parameters, but the subclass changes what it does.
Overriding is one of the main ways child classes specialize inherited behavior. It is also what makes runtime polymorphism work in Java.
Basic Example
In this example, Dog extends Animal and overrides the speak() method.
class Animal {
void speak() {
System.out.println("The animal makes a sound.");
}
}
class Dog extends Animal {
@Override
void speak() {
System.out.println("The dog says woof.");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal();
Dog dog = new Dog();
animal.speak();
dog.speak();
}
}
Output:
The animal makes a sound.
The dog says woof.
The Dog class has a method with the same name, return type, and parameter list as the method in Animal. When dog.speak() runs, Java uses the Dog version.
The @Override Annotation
The @Override annotation is not required, but you should use it when you intend to override a method. It asks the compiler to check your work.
If you misspell the method name or change the parameters by accident, Java will report an error instead of silently creating a different method.
Calling The Parent Version With super
An overriding method can call the original superclass method with super.methodName(). This is useful when the child class wants to add behavior before or after the inherited behavior.
class Message {
void print() {
System.out.println("Preparing message...");
}
}
class EmailMessage extends Message {
@Override
void print() {
super.print();
System.out.println("Sending email message.");
}
}
public class Main {
public static void main(String[] args) {
EmailMessage message = new EmailMessage();
message.print();
}
}
Output:
Preparing message...
Sending email message.
The EmailMessage method first runs Message‘s print() method. Then it runs its own extra line.
Overriding Rules
To override a method correctly, the subclass method must match the inherited method’s signature. The signature includes the method name and parameter list.
- The method name must be the same.
- The parameter types and order must be the same.
- The return type must be the same, or a compatible subclass return type for object returns.
- The overriding method cannot be less accessible than the parent method.
private,static, andfinalmethods are not overridden in the usual instance-method sense.
Overriding With Polymorphism
When a superclass variable refers to a subclass object, Java still runs the overridden method from the actual object type.
class Shape {
void draw() {
System.out.println("Drawing a shape.");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle.");
}
}
public class Main {
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw();
}
}
Output:
Drawing a circle.
The variable type is Shape, but the object created with new Circle() is a Circle. At runtime, Java chooses the Circle version of draw().
Overriding Vs Overloading
Overriding and overloading are different ideas. Overriding replaces an inherited method in a subclass. Overloading creates methods with the same name but different parameter lists, usually in the same class.
If you accidentally change the parameters while trying to override, you are creating an overload instead. That is why @Override is helpful.
Common Mistakes
- Changing the parameter list and expecting the parent method to be overridden.
- Leaving off
@Override, which makes mistakes harder to catch. - Trying to override a
finalmethod. - Making the child method more restrictive, such as changing a
publicmethod to package access.
Takeaway: method overriding lets a subclass keep the same method contract while providing behavior that fits the subclass.
