Knowee
Questions
Features
Study Tools

class Animal {    public String speak() {        return "Some sound";    }}class Dog extends Animal {    @Override    public String speak() {        return "Bark";    }}class Cat extends Animal {    @Override    public String speak() {        return "Meow";    }}public class TestAnimals {    public static void main(String[] args) {        Animal myAnimal = new Animal();        Animal myDog = new Dog();        Animal myCat = new Cat();        System.out.println(myAnimal.speak());        System.out.println(myDog.speak());        System.out.println(myCat.speak());    }}a.Compilation Errorb.Some soundBarkMeowc.Some soundSome soundSome soundd.BarkBarkMeow

Question

class Animal {    public String speak() {        return "Some sound";    }}class Dog extends Animal {    @Override    public String speak() {        return "Bark";    }}class Cat extends Animal {    @Override    public String speak() {        return "Meow";    }}public class TestAnimals {    public static void main(String[] args) {        Animal myAnimal = new Animal();        Animal myDog = new Dog();        Animal myCat = new Cat();        System.out.println(myAnimal.speak());        System.out.println(myDog.speak());        System.out.println(myCat.speak());    }}a.Compilation Errorb.Some soundBarkMeowc.Some soundSome soundSome soundd.BarkBarkMeow

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct answer is b. Some sound Bark Meow

Here's the step by step explanation:

  1. The Animal class has a method speak() that returns "Some sound".
  2. The Dog class extends Animal and overrides the speak() method to return "Bark".
  3. The Cat class also extends Animal and overrides the speak() method to return "Meow".
  4. In the main method of the TestAnimals class, three objects are created: myAnimal of type Animal, myDog of type Dog, and myCat of type Cat. However, they are all declared as type Animal.
  5. When the speak() method is called on myAnimal, the method from the Animal class is used, so "Some sound" is printed.
  6. When the speak() method is called on myDog, the method from the Dog class is used (because of method overriding), so "Bark" is printed.
  7. When the speak() method is called on myCat, the method from the Cat class is used (because of method overriding), so "Meow" is printed.

This problem has been solved

Similar Questions

What is output of the following code ?class Animal {void makeSound(){System.out.prntln("Animal sound");}}class Dog extends Animal {void makeSound(){System.out.println("Bark");}}public class Test{public static void main(String[] args) {Animal a = new Dog();a.makeSound();}}Question 19Answera.Runtime errorb.Barkc.Compilation errord.Animal soundClear my choice

What is the expected output?class Animal { Animal() { System.out.println("Animal"); }}class Wild extends Animal{ Wild() { System.out.println("Wild"); super(); }}public class Test { public static void main(String args[]) { Wild wild = new Wild(); }}

class FourWheeler{    public FourWheeler()    {        System.out.println("Class FourWheeler");    }}class Car extends FourWheeler{    public Car()     {        System.out.println("Class Car");        }}class Audi extends Car{   public Audi()    {        super();        System.out.println("Class Audi");    }}class Driver{    public static void main(String args[])    {        Audi cc=new Audi();    }}

B A class A  {    String name="A";    public String getName()  {        return name;    }      String greeting()  {        return "class A";    }}class B extends A  {    String name="B";          String greeting()  {        return "class B";    }  }public class Test   {    public static void main(String arg[])  {        A a=new A();        A b=new B();        System.out.println(a.greeting()+" has name "+a.getName());        System.out.println(b.greeting()+" has name "+b.getName());    }} Place the names "A" and "B" in the following output. class has name                            class has name

what is the result of the following piece of code:public class Person{ public void talk(){ System.out.print("I am a Person"); }}public class Student extends Person{ public void talk(){ System.out.print("I am a Student"); }}public class Test{ public static void main(String args[]){ Person p = new Student(); p.talk(); }}I am a Person I am a Student I am a Person I am a StudentI am a Student I am a Person

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.