In Java, for the syntax fragment "X extends Y", which of the following statements about classes and interfaces is true?

Difficulty: Medium

Correct Answer: "X extends Y" is correct when X and Y are both classes or both interfaces, but not when one is a class and the other is an interface

Explanation:


Introduction / Context:
Java uses the keywords extends and implements to describe type relationships between classes and interfaces. Remembering which combinations are legal is essential for designing inheritance hierarchies and implementing interfaces. Interviewers often test this by presenting a generic form like "X extends Y" and asking when it is valid, forcing you to recall the rules for classes and interfaces separately.


Given Data / Assumptions:

  • X and Y are Java types that may be classes or interfaces.
  • The syntax "X extends Y" appears in a type declaration.
  • We are not considering multiple inheritance of classes, which Java does not allow.
  • We assume standard Java 8 or later behaviour for interfaces and classes.


Concept / Approach:
In Java, a class extends another class and implements one or more interfaces. That means that for classes, the keyword extends is used to express single inheritance from a base class. For interfaces, the keyword extends is also used, but here it expresses interface inheritance from one or more superinterfaces. An interface never uses implements to inherit from another interface; it still uses extends. A class never extends an interface; it implements an interface instead. Therefore, "X extends Y" is legal if X and Y are both classes or if X and Y are both interfaces, but not if one is a class and the other is an interface.


Step-by-Step Solution:
Step 1: Consider the case where both X and Y are classes, for example class B extends A. This is correct; Java allows a class to extend exactly one superclass. Step 2: Consider the case where both X and Y are interfaces, for example interface B extends A. This is also correct; interfaces can extend one or more interfaces. Step 3: Consider the case where X is a class and Y is an interface, for example class C extends I. This is incorrect; a class must use implements when adopting an interface. Step 4: Consider the case where X is an interface and Y is a class, for example interface I extends C. This is also incorrect; interfaces cannot extend classes. Step 5: Combine these observations to see that "X extends Y" only works when X and Y are of the same kind: both classes or both interfaces.


Verification / Alternative check:
A quick check is to recall familiar forms from real Java code. You have seen class ArrayList extends AbstractList and interface List extends Collection. You have also seen class ArrayList implements List. You have not seen class Something extends InterfaceName nor interface Something extends ClassName, because these are forbidden. The compiler will emit errors if you attempt such definitions. This real world pattern confirms that extends is used for class to class and interface to interface relationships, while class to interface relationships use implements instead.


Why Other Options Are Wrong:
Option "X extends Y" is correct only when both X and Y are classes: This ignores the legal and common case where one interface extends another interface. Option "X extends Y" is correct only when X is an interface and Y is a class: This is invalid syntax; interfaces cannot extend classes. Option "X extends Y" is always correct for any combination of class and interface types: This is clearly false; Java enforces specific rules about when extends and implements may be used.


Common Pitfalls:
A frequent mistake is to forget that interfaces also use extends rather than implements when inheriting from other interfaces. Another pitfall is to think that, because interfaces and classes are both types, you can arbitrarily mix extends and implements between them. Remembering a simple rule helps: class extends class and implements interface; interface extends interface only. Keeping this rule in mind avoids many inheritance syntax errors.


Final Answer:
The correct statement is that "X extends Y" is correct when X and Y are both classes or both interfaces, but not when one is a class and the other is an interface.

More Questions from Technology

Discussion & Comments

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