Exercises


Note

The solutions to these exercises are in Appendix B.

  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.)

  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.

  3. Given the following interface:

    interface InterfaceQ3 {   void printALine(); } 

    Will the following code compile?

    class ClassQ3 implements InterfaceQ3 {   void printALine()   {     System.out.println("OK");   } }
  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);   } } 




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