Tuesday, August 24, 2010

Overriding: FWD.java

Courtesy of Sun Web Learning Center

Given:

 1. class FWD {
 2.   int doMud(int x) { return 1; }
 3. }
 4. class Subaru extends FWD {
 5.   int doMud(int... y) { return 2; }
 6.   int doMud(long z) { return 3; }
 7. }
 8. class Race {
 9.   public static void main(String [] args) {
10.     FWD f = new Subaru();
11.     System.out.println(f.doMud(7));
12.   }
13. }

What is the result?


A 1
B 2
C 3
D 7
E Compilation fails.
F The output is NOT predictable.

A is correct. The method on line 2 matches the call made on line 11. The method on line 5 has a different signature.

No comments:

Post a Comment