Consider the following Java interface: public abstract interface Frobnicate { public void twiddle(String s); } Which of the following Frob class declarations is correct with respect to this interface?

Difficulty: Medium

Correct Answer: public abstract class Frob implements Frobnicate { }

Explanation:


Introduction / Context:
This question tests your knowledge of Java interfaces, abstract classes and method implementation rules. You are given an interface Frobnicate with a single method twiddle that takes a String parameter. You must determine which Frob class declaration is legal and consistent with Java interface implementation rules.


Given Data / Assumptions:

    Interface Frobnicate declares public void twiddle(String s).
    Any concrete class that implements Frobnicate must provide a matching implementation of twiddle with a String parameter.
    An abstract class that implements an interface may leave some or all methods unimplemented, keeping them abstract.
    The options include different combinations of extends, implements and method signatures.


Concept / Approach:
In Java, interfaces are implemented using the implements keyword, not extends. A class that implements an interface must either provide concrete implementations of all interface methods or be declared abstract. If a class declares a method as abstract, it cannot provide a method body for that method. In addition, method signatures must match exactly, including parameter types, in order to fulfill an interface contract.


Step-by-Step Solution:
Check option a: it declares an abstract class and declares twiddle as abstract but also gives a method body, which is illegal in Java. An abstract method must not have a body. Check option b: it declares an abstract class that implements Frobnicate but does not define twiddle. This is legal, because abstract classes may leave interface methods abstract for subclasses to implement. Check option c: it tries to extend Frobnicate, which is wrong because Frobnicate is an interface and classes should implement interfaces, not extend them. Check option d: it declares a concrete class that implements Frobnicate but defines twiddle with an Integer parameter rather than a String parameter, which does not satisfy the interface method requirement. Conclude that option b is the only correct declaration.


Verification / Alternative check:
If you attempt to compile option b in a Java compiler, it will succeed, although any nonabstract subclass of Frob will need to implement twiddle. If you compile options a, c or d, the compiler will produce errors indicating either illegal abstract method syntax, incorrect use of extends or failure to implement the required method with the correct signature.


Why Other Options Are Wrong:
Option a mixes abstract with a method body, which Java does not allow.
Option c misuses extends with an interface and defines the wrong method signature.
Option d uses implements but also defines the wrong parameter type, so it does not actually implement twiddle as required.


Common Pitfalls:
Many learners confuse extends and implements when working with interfaces. Another common error is to change parameter types or method names slightly and assume that they still satisfy the interface contract. When implementing interfaces, always ensure that method names, parameter lists and return types match exactly, and remember that abstract classes can defer implementation to subclasses.


Final Answer:
The correct class declaration is public abstract class Frob implements Frobnicate { } which legally implements the interface while remaining abstract.

More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion