Question

Consider the following Java code: class Animal {     String type = "Generic Animal";     void eat() {         System.out.println("Animal eats food.");     } } class Dog extends Animal {     String type = "Dog";     void bark() {         System.out.println("Dog barks.");     } } public class TestInheritance {     public static void main(String[] args) {         Dog myDog = new Dog();         System.out.println(myDog.type);         myDog.eat();         myDog.bark();     } } What will be the output of this code?

A Dog\nAnimal eats food.\nDog barks.
B Generic Animal\nAnimal eats food.\nDog barks.
C Dog\nDog barks.\nAnimal eats food.
D Generic Animal\nDog barks.\nAnimal eats food.
E A compile-time error.
Practice Next

Hey! Ask a query