Which is true? (Choose all that apply. )
-
A. "X extends Y" is correct if and only if X is a class and Y is an interface
-
B. "X extends Y" is correct if and only if X is an interface and Y is a class
-
C. "X extends Y" is correct if X and Y are either both classes or both interfaces
-
D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces
In Java, the extends keyword is used for inheritance between compatible types. A class can only extend another class, and an interface can only extend another interface. A class cannot extend an interface (it must use implements), and an interface cannot extend a class, making the other options incorrect.
To answer this question, let's go through each option to understand why it is correct or incorrect:
Option A) "X extends Y" is correct if and only if X is a class and Y is an interface - This option is incorrect. In Java, the keyword "extends" is used to indicate that a class is inheriting from another class or implementing an interface. Therefore, "X extends Y" is correct if X is a class and Y is either a class or an interface.
Option B) "X extends Y" is correct if and only if X is an interface and Y is a class - This option is incorrect. In Java, interfaces cannot directly inherit from classes. Instead, they can extend other interfaces. Therefore, "X extends Y" is incorrect if X is an interface and Y is a class.
Option C) "X extends Y" is correct if X and Y are either both classes or both interfaces - This option is correct. In Java, both classes and interfaces can extend other classes or interfaces. Therefore, "X extends Y" is correct if X and Y are either both classes or both interfaces.
Option D) "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces - This option is incorrect. As mentioned before, interfaces cannot directly extend classes in Java. Therefore, "X extends Y" is not correct if X is an interface and Y is a class.
The correct answer is C. "X extends Y" is correct if X and Y are either both classes or both interfaces.