Complete the code to declare a class that inherits from another class.
class Dog extends [1] {}
In Java, a class inherits from another class using the extends keyword followed by the parent class name. Here, Dog inherits from Animal.
Complete the code to call the parent class constructor from the child class.
class Dog extends Animal { Dog() { super[1]; } }
super.The super() call invokes the parent class constructor. Parentheses are needed to call it as a method.
Fix the error in the code to override a method from the parent class.
class Dog extends Animal { @Override public void [1]() { System.out.println("Dog barks"); } }
@Override annotation.The method speak() is overridden from the parent class Animal. The method name must match exactly to override.
Fill both blanks to create a subclass that inherits and adds a new method.
class [1] extends Animal { public void [2]() { System.out.println("Runs fast"); } }
The class Cheetah inherits from Animal and adds a new method runFast() to show its behavior.
Fill all three blanks to complete the code that demonstrates inheritance and method overriding.
class [1] { public void sound() { System.out.println("Animal sound"); } } class [2] extends [3] { @Override public void sound() { System.out.println("Meow"); } }
The Cat class inherits from Animal and overrides the sound() method to print "Meow" instead of the generic animal sound.