Junior — Middle
Is it possible to change the implementation of a method in a derived class?
sobes.tech AI
Answer from AI
Yes, in Java, it is possible to override a method in a subclass — this is called method overriding. To do this, you declare a method with the same signature in the subclass and provide a new implementation. The method in the superclass should not be declared as final.
Example:
class Animal {
void sound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark");
}
}
In this example, the sound() method is overridden in the Dog class.