Basic Inheritance in Java: Explanation & Practice

Create a child class that inherits from parent

Problem summary

Create an Animal class and a Dog class that inherits from it.

Starter code

// Create Animal class with:
// - void eat() prints "Eating..."

// Create Dog class extending Animal with:
// - void bark() prints "Woof!"

public class Main {
    public static void main(String[] args) {
        // Create dog and call both methods
    }
}

Expected output and test cases

  • Dog eats and barks
    Eating...
    Woof!

Hints

  1. Use 'extends' keyword for inheritance
  2. Child class gets parent's methods
  3. Dog inherits eat() from Animal

Related Inheritance exercises

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