Given the class definitions:
And the code fragment of the main() method,
What is the result?
A. Java
Java
Java
B. Java
Jeve va
C. Java
Jeve ve
D. Compilation fails
Given the class definitions:
And the code fragment of the main() method,
What is the result?
A. Java
Java
Java
B. Java
Jeve va
C. Java
Jeve ve
D. Compilation fails
Correct answer is B. as DonBa says:
class Alpha {
public String doStuff(String msg) {
return msg;
}
}
class Beta extends Alpha {
public String doStuff(String msg) {
return msg.replace(‘a’, ‘e’);
}
}
class Gamma extends Beta {
public String doStuff(String msg) {
return msg.substring(2);
}
}
public class Test {
public static void main(String[] args) {
List strs = new ArrayList();
strs.add(new Alpha());
strs.add(new Beta());
strs.add(new Gamma());
for (Alpha t : strs) {
System.out.println(t.doStuff(“Java”));
}
}
}
Output:
Java
Jeve
va
Correct answer is B.