Saturday, August 21, 2010

Inheritance: Acid.java

Courtesy of Sun Web Learning Center

 4. class Chemical {
 5.   int ph() { return 7; }
 6. }
 7. public class Acid {
 8.   public static void main(String[] args) {
 9.     new Acid().go();
10.   }
11.   void go() { 
12.     System.out.println(ph() + " " + super.ph() + " " + getPh());
13.   }
14.   int getPh() { return 4; }
15.   int ph() { return 3; }
15. }

What is the result?


A     3 7 4
B     4 7 4
C     7 7 4
D     Compilation fails
E     An exception is thrown at runtime

D is correct. Acid does not extend Chemical.

No comments:

Post a Comment