In Java, method overriding is a feature that allows a subclass to provide a specific implementation for a method that is already defined in its superclass. This is a key concept in object-oriented programming (OOP) and is essential for achieving polymorphism in Java. When a method in a subclass has the same signature as a method in its superclass, the subclass method is said to override the superclass method.
This guide will help you understand what method overriding is, its syntax, how it works, and provide practical examples to illustrate the concept.
super
Keyword with Method OverridingMethod overriding in Java allows a subclass to redefine the behavior of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass to override it.
Overriding is used when the behavior of an inherited method needs to be customized for the subclass. This provides more specific behavior than the superclass method while maintaining the same method signature.
The syntax for overriding a method in Java is quite simple. Here's the basic structure:
class Superclass {
// Method in the superclass
void display() {
System.out.println("Display in Superclass");
}
}
class Subclass extends Superclass {
// Overriding the method in the subclass
@Override
void display() {
System.out.println("Display in Subclass");
}
}
In the above example:
display()
is defined in both the superclass and the subclass.display()
method, which overrides the superclass method.@Override
Annotation: While optional, it is good practice to use the @Override
annotation in the subclass to indicate that the method is intended to override a method in the superclass.public
, the subclass method must be public
or more permissive.Let’s look at a practical example to understand method overriding.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Main {
public static void main(String[] args) {
Animal animal = new Animal();
animal.sound(); // Output: Animal makes a sound
Dog dog = new Dog();
dog.sound(); // Output: Dog barks
// Polymorphism example
Animal animalDog = new Dog();
animalDog.sound(); // Output: Dog barks
}
}
Animal makes a sound
Dog barks
Dog barks
In this example:
sound()
method is overridden in the Dog
class to provide a specific implementation.Animal
is used to call the sound()
method on an object of type Dog
.When overriding a method, the following rules must be followed:
protected
, the overridden method can be protected
or public
, but not private
.super
Keyword with Method OverridingThe super
keyword is used to call the superclass method from within the subclass. It can be useful when you want to use the overridden method in addition to providing specific behavior in the subclass.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
super.sound(); // Calls the superclass method
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
Animal makes a sound
Dog barks
Here, the super.sound()
statement calls the sound()
method from the Animal
class before executing the sound()
method in the Dog
class.
Polymorphism is one of the main benefits of method overriding. When an object of a subclass is referred to by a superclass reference, the method in the subclass is invoked at runtime. This is known as runtime polymorphism.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Main {
public static void main(String[] args) {
Animal animal = new Dog(); // Animal reference but Dog object
animal.sound(); // Output: Dog barks
}
}
Dog barks
In this case, even though the reference is of type Animal
, the method sound()
from the Dog
class is called due to method overriding.
public
method with a private
method.@Override
: While optional, forgetting to use the @Override
annotation can lead to mistakes, such as mismatched method signatures or typos.