Implementing class hierarchy for animals and sounds

Report a typo

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.

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);
    }
}
Dogextendsprotected
___

Create a free account to access the full topic