Inheritance and method calling in animal hierarchy

Report a typo

Given the provided code which represents a simple inheritance hierarchy in Java, fill the blanks to have the right access to the methods. There are two classes involved - a parent class 'Animal' and a child class 'Dog'. The 'Animal' class has a method 'saySomething()'. It is inherited by the 'Dog' class which has its own method 'bark()'. This method should call 'saySomething()' method and append the string ' Woof!' to it. Complete the code such that the method 'saySomething()' of 'Animal' class is correctly accessible from the 'Dog' class and the program prints 'Animal sound! Woof!' when executed.

Fill in the gaps with the relevant elements
public class  {
     String saySomething() {
        return "Animal sound!";
    }
}

public class   Animal {
    public String bark() {
        return saySomething() + " Woof!";
    }
}

public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog();
        System.out.println(dog.bark());
    }
}
AnimalextendsprotectedDog
___

Create a free account to access the full topic