Method overriding is one of the most important concepts in Java Object-Oriented Programming (OOP). It allows a child class to provide its own implementation of a method that is already defined in the parent class.
Example of Method Overriding:
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
Output:
Dog barks
Top comments (0)