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.
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClasses and objectsAccess control
Protected modifier
Inheritance and method calling in animal hierarchy
Report a typo
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());
}
} ___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.