Yes, a subclass can override a method of the superclass. When a subclass defines a method with the same name, return type, and parameters as a method of the superclass, it is said to override the method of the superclass. The subclass's method implementation will be called instead of the superclass method implementation when the method is invoked on an object of the subclass.
Example Code:
class Animal {
void eat() {
System.out.println("The animal is eating");
}
}
class Dog extends Animal {
@Override
void eat() {
System.out.println("The dog is eating");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // The dog is eating
}
}
In the above code, the Dog class overrides the eat() method of the Animal class. When the eat() method is called on an object of the Dog class, the Dog class's implementation of the method is called, which prints "The dog is eating" to the console.