Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The output of the code will be "Bark". This is because the object 'a' is an instance of the Dog class, which has overridden the makeSound() method of the Animal class. So, when we call a.makeSound(), the makeSound() method of the Dog class is executed.

Similar Questions

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

Given:class FourWheeler { public FourWheeler () { System.out.print(1); } } class Car extends FourWheeler{ public Car() { System.out.print(2); } } class Audi extends Car{ public Audi() {System.out.print(3); } } public class Driver{ public static void main( String[] argv ) {new Audi(); } } What is the result when this code is executed?Select one:a.The code runs with no outputb.321c.123d.3

What is the output of the following code snippet?class Parent {    static void print() {        System.out.println("Parent");    }}class Child extends Parent {    static void print() {        System.out.println("Child");    }}public class Main {    public static void main(String[] args) {        Parent obj = new Child();        obj.print();    }}Question 3Answera.Parentb.Runtime errorc.Childd.Compilation error

What will be the output of the following code snippet?public class MyClass {    static int value = 10;      public void printValue() {        System.out.println(value);    }        public static void main(String[] args) {        MyClass obj = new MyClass();        obj.printValue();    }}Question 4Answera.0b.10c.Compilation errord.Runtime exception

What is the output of the following code? 1: public abstract class Whale { 2: public abstract void dive() {}; 3: public static void main(String[] args) { 4: Whale whale = new Orca(); 5: whale.dive(); 6: } 7: }    8: class Orca extends Whale { 9: public void dive(int depth) { System.out.println("Orca diving"); } 10: }*1 pointA. Orca divingB. The code will not compile because of line 2.C. The code will not compile because of line 8.D. The code will not compile because of line 9.E. The output cannot be determined from the code provided.

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.