Method Overriding in Java: Explanation & Practice
Override a parent method in child class
Problem summary
Override the speak() method in Cat to make it say 'Meow' instead of generic 'Sound'.
Starter code
// Create Animal with:
// - void speak() prints "Sound"
// Create Cat extending Animal:
// - Override speak() to print "Meow"
public class Main {
public static void main(String[] args) {
// Create both and call speak()
}
}Expected output and test cases
- Override demonstration
Sound Meow
Hints
- Use @Override annotation
- Same method signature as parent
- Child version is called for child objects
Related Inheritance exercises
- Practice Basic Inheritance in Java
- Practice Using Super Keyword in Java
- Practice Abstract Classes in Java
Practice all Inheritance exercises · Run this idea in the Java compiler