Junior — Middle
Can you explain what method overriding means in object-oriented programming?
sobes.tech AI
Answer from AI
Method overriding is a mechanism in object-oriented programming where a subclass provides its own implementation of a method that is already defined in its superclass.
This allows modifying or extending the behavior of inherited methods for specific subclass needs.
Example in Java:
class Animal {
void sound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark");
}
}
Here, the sound() method in the Dog class overrides the method from Animal to produce a sound characteristic of a dog.