Friday, August 6, 2010

Abstract Interface: Frobnicate

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Chapter 2, Exercise 1

Given:


public abstract interface Frobnicate {
   public void twiddle(String s);
}


Which is a correct class? (Choose all that apply.)

A.

public abstract class Frob implements Frobnicate {
   public abstract void twiddle(String s) { }
}

B.

public abstract class Frob implements Frobnicate { }

C.

public class Frob extends Frobnicate {
   public void twiddle(Integer i) { }
}

D.

public class Frob implements Frobnicate {
   public void twiddle(Integer i) { }
}

E.

public class Frob implements Frobnicate {
   public void twiddle(String i) { }
   public void twiddle(Integer s) { }
}



Explanation

A. Incorrect because abstract methods have no body

public abstract class Frob implements Frobnicate {
   public abstract void twiddle(String s) { }
}

B. Correct; an abstract class need not implement any or all of an interface's methods

C. Incorrect; classes implement interfaces they don't extend them.

public class Frob extends Frobnicate {
   public void twiddle(Integer i) { }
}

D. Incorrect. Even though it implements the interface, it merely overloads the twiddle() method, NOT actually implementing it. If we would have either kept this class as abstract or implemented twiddle(), then D would be a correct option.

public abstract class Frob implements Frobnicate {
   public void twiddle(Integer i) { }
}

OR

public class Frob implements Frobnicate {
   public void twiddle(Integer i) { }
   public void twiddle(String i) { }
}

E. Correct because this class implements twiddle(String i)

No comments:

Post a Comment