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
- Use 'extends' keyword for inheritance
- Child class gets parent's methods
- Dog inherits eat() from Animal
Related Inheritance exercises
- Practice Method Overriding in Java
- Practice Using Super Keyword in Java
- Practice Abstract Classes in Java
Practice all Inheritance exercises · Run this idea in the Java compiler