Method Overriding
Method Overriding :
Explanation:
Method overriding allows a subclass to provide a specific implementation for a method defined in its superclass.
Example:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound.");
}
}
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Cat meows.");
}
}
public class Main {
public static void main(String[] args) {
Animal myCat = new Cat();
myCat.makeSound(); // Output: Cat meows.
}
}
Comments
Post a Comment