Chapter 10


Exercise 1 Suppose an interface declares three methods. And suppose a class declares that it implements the interface, but in fact it only implements two out of the three methods. What happens when you try to compile the class? (The way to answer this question, of course, is to write an interface and a class.)

Solution 1 You get a compilation error that says your class must be declared abstract. This is a perfectly sensible requirement. The following interface declares three methods:

interface Q1Inter {   public void a();   public void b();   public void c(); }

The following class does not completely implement the interface:

class Q1Class implements Q1Inter {   public void a()   {     System.out.println("Method a()");   }   public void b()   {     System.out.println("Method b()");   } }

When you compile, you get the following message or something very similar: "Class Q1Class should be declared abstract; it does not define method c() in interface Q1Inter."

Exercise 2 If class A implements an interface, any subclasses of A inherit all the methods specified in the interface. Does this mean that subclasses of A also implement the interface? Write code to discover the answer.

Solution 2 First, let's define the interface:

interface Q2Inter {   public void x(); }

Now here's a superclass that implements the interface:

class Q2Superclass implements Q2Inter {   public void x()   {     System.out.println("Hello from X.");   } }

And here's a subclass:

class Q2Subclass extends Q2Superclass { }

The subclass does not explicitly declare that it implements the interface, but it inherits an implementation of x() from its parent class. Does the compiler believe that Q2Subclass implements Q2Inter? Let's add some test code somewhere. We need a main() method, and we might as well put it in Q2Subclass:

class Q2Subclass extends Q2Superclass {   public static void main(String[] args)   {     Q2Subclass subby = new Q2Subclass();     if (subby instanceof Q2Inter)       System.out.println("It implements.");     else       System.out.println("It does not implement.");   } }

The application prints out "It implements", indicating that the subclass implicitly implements the interface declared explicitly by the superclass. In other words, interface implementation is a property that is inherited by subclasses.

Exercise 3 Given the following interface:

interface InterfaceQ3 {   void printALine(); } 

Will the following code compile?

class ClassQ3 implements InterfaceQ3 {   void printALine()   {     System.out.println("OK");   } }

Solution 3 The code will not compile. The sources don't use explicit access modifiers. In the class code, this means printALine() has default access. But all methods (and constants) in an interface are public. The error message is something like this:

Method printALint() in class ClassQ3 cannot implement method printALint() in interface InterfaceQ3 with weaker access privileges, was public…

Exercise 4 Don't worry, the following question requires absolutely no understanding of physics. In fact, it might make you grateful that you chose computer programming instead. Suppose you have the following interface:

package physics; interface PhysicsConstants {   public static final double ELECTRON_MASS_KG = 9.11e-31;   public static final double            STEFAN_BOLTZMANN_CONSTANT_WATTS_PER_M2 = 5.67e-8; }

What does the following application print out?

package physics; public class Q4 implements PhysicsConstants {   public static void main(String[] args)   {     System.out.println("The value is " +       STEFAN_BOLTZMAN_CONSTANT_WATTS_PER_M2);   } }

Solution 4 Trick question. The code doesn't print out anything, because it does not compile. There are two n's in "Boltzmann", but in the main() method there is only one.

The point of this question is to show that human eyes aren't the best mechanism for catching typos in long strings. When you try to compile the application, the compiler immediately finds the typo for you and directs you to the line you need to fix. If you used literal numerical values instead, you would be typing a much shorter string. "5.67e-8" only has 7 characters, versus 38 in "STEFAN_BOLTZMANN_CONSTANT_WATTS_PER_M2", so the odds of a typo are 7/38 what they would be if you used the named constant. However, if you type "5.67e-8" enough times, you are bound to make a mistake eventually, and the effort of finding the typo would more than cancel out the time you saved by typing the shorter literal numeric value.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net