Polymorphism and Method Overriding
Consider the following code. How many times will the body of the for loop execute when the main method is run?
class Parent {
public void runLoop() {
for (int i = 1; i <= 3; i++) {
System.out.print(i + " ");
}
}
}
class Child extends Parent {
@Override
public void runLoop() {
for (int i = 0; i < 4; i++) {
System.out.print(i + " ");
}
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
p.runLoop();
}
}
A
4
B
3
C
7
D
0
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE