0%
0 / 13 answered
Anatomy of a Class Practice Test
•13 QuestionsQuestion
1 / 13
Q1
Considering the Java class example below, which statement best describes polymorphism?
public class Animal {
public void speak() {
System.out.println("...");
}
}
public class Dog extends Animal {
@Override
public void speak() {
System.out.println("Woof");
}
}
public class Cat extends Animal {
@Override
public void speak() {
System.out.println("Meow");
}
}
class Demo {
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.speak();
a2.speak();
}
}
// Encapsulation: hide fields using private.
// Inheritance: Dog and Cat extend Animal.
// Polymorphism: the same call can run different overridden methods.
Based on the class structure presented above, which best describes polymorphism here?
Considering the Java class example below, which statement best describes polymorphism?
public class Animal {
public void speak() {
System.out.println("...");
}
}
public class Dog extends Animal {
@Override
public void speak() {
System.out.println("Woof");
}
}
public class Cat extends Animal {
@Override
public void speak() {
System.out.println("Meow");
}
}
class Demo {
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.speak();
a2.speak();
}
}
// Encapsulation: hide fields using private.
// Inheritance: Dog and Cat extend Animal.
// Polymorphism: the same call can run different overridden methods.
Based on the class structure presented above, which best describes polymorphism here?