What is printed by the following code. public class Inherit { public static void
ID: 3575334 • Letter: W
Question
What is printed by the following code. public class Inherit { public static void main(String! ] args) { Speaker d = new Dog( ); Speaker c = new Cat( ); d.speak( ); c.speak ( ); } } public abstract class Speaker { abstract public void speak ( ); } } public class Cat extends Speaker { public void speak( ) { System.out.println("Woof!"); } } public class Dog extends Speaker { public void speak( ) { System.out.println("Meow!"); } } Be sure that you carefully examine or trace through the code to see what path the execution will take and what each speak method actually does. Woof! Meow Meow! Woof! Meow! Meow! Woof! Woof! none of the aboveExplanation / Answer
1. There is an abstract by name Speaker with an abstract method speak.
2. Class Cat extends the abstract class Speaker and overrides the method speak by printing a statement "Woof!"
3. Class Dog extends the abstract class Speaker and overrides the method speak by printing a statement "Meow!"
4. In main function, two Speaker class objects are defined (d,c).
5. Object d is instantiated with child class Dog.
6. Object c is instantiated with child class Cat.
7. Statement d.speak() will print the statement "Meow!"
8. Statement c.speak() will print the statement "Woof!"
So the output will be:
Meow!
Woof!
Correct option is (B)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.