Imagine that we are creating a simple class hierarchy representing animals and their sounds. The base class is 'Animal' with a generic 'sound'. A subclass called 'Dog' extends 'Animal' and has a specific 'sound', which is 'Bark'. In the 'Main' class, an object of type 'Animal' is created, but it is actually a 'Dog'. Fill in the blanks in the provided code to display 'Bark' on the console.
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClass hierarchiesBuilding class hierarchies
Referencing subclass objects
Implementing class hierarchy for animals and sounds
Report a typo
Fill in the gaps with the relevant elements
public class Animal {
String sound = "Animal Sound";
}
public class Animal {
protected String sound = "Bark";
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
System.out.println(((Dog)animal).sound);
}
} ___
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.