Given the code fragment: What is the result?
A.
Contract Contract Super
B.
Compilation fails at line n1
C.
Super Sub Sub
D.
Compilation fails at line n2
Given the code fragment: What is the result?
A.
Contract Contract Super
B.
Compilation fails at line n1
C.
Super Sub Sub
D.
Compilation fails at line n2
Answer: C
Code question:
import java.util.ArrayList;
public class Ref {
public static void main(String[] args) {
ArrayList objs = new ArrayList();
Contract c1 = new Super();
Contract c2 = new Sub(); //ln1
Super s1 = new Sub();
objs.add(c1);
objs.add(c2);
objs.add(s1); //ln2
for (Object itm: objs) {
System.out.println(itm.getClass().getName());
}
}
}
public interface Contract {}
public class Super implements Contract{}
public class Sub extends Super {}
Output:
Super
Sub
Sub
BUILD SUCCESSFUL (total time: 0 seconds)