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

  1. Use @Override annotation
  2. Same method signature as parent
  3. Child version is called for child objects

Related Inheritance exercises

Practice all Inheritance exercises · Run this idea in the Java compiler