176k views
4 votes
What is the output of the following code? public class Inherit { abstract class Speaker { abstract public void speak(); } class Cat extends Speaker { public void speak() { System.out.println("Woof!"); } } class Dog extends Speaker { public void speak() { System.out.println("Meow!"); } } Inherit() { Speaker d = new Dog(); Speaker c = new Cat(); d.speak(); c.speak(); } public static void main(String[] args) { new Inherit(); } }

User Murthi
by
5.7k points

1 Answer

6 votes

Answer:

The answer is "Meow! Woof!"

Step-by-step explanation:

The description of the java program can be given as:

  • In the java program, a class Inherit is defined inside a class an abstract class that is "speak" is defined that contains an abstract method that is "speak".
  • In the next step we define two classes that are "Cat and Dog" both classes inherit the abstract class and its method. Cat class use the abstract method speaker and write a message that is "Woof!" and the Dog class use this method and write a message that is "Meow!".
  • In the next step, create an Inherit class constructor and inside this constructor, we create an instance and call both classes.
  • In the main method, we create an Inherit class instance.

User Malathi
by
5.4k points