Multiple choice java

Which of the following statement is true?

  1. X extends Y is correct if and only if X is a class and Y is an interface

  2. X extends Y is correct if and only if X is an interface and Y is a class

  3. X extends Y is correct if X and Y are either both classes or both interfaces

  4. X extends Y is correct for all combinations of X and Y being classes and/or interfaces.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The 'extends' keyword is used for inheritance. A class can extend another class (single inheritance). An interface can extend another interface (multiple inheritance allowed). A class cannot extend an interface (it implements it), and an interface cannot extend a class.

AI explanation

To answer this question, you need to understand the concept of inheritance in object-oriented programming.

Option A) X extends Y is correct if and only if X is a class and Y is an interface - This option is incorrect because in Java, a class can extend another class or implement an interface. It is not limited to only extending classes and implementing interfaces.

Option B) X extends Y is correct if and only if X is an interface and Y is a class - This option is incorrect because in Java, a class can extend another class or implement an interface. It is not limited to only extending interfaces and implementing classes.

Option C) X extends Y is correct if X and Y are either both classes or both interfaces - This option is correct. In Java, a class can extend another class and implement multiple interfaces. Similarly, an interface can extend multiple interfaces. So, X extends Y is correct if both 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 because, as explained above, extending a class and implementing an interface are two different concepts in Java. The correct statement is that X extends Y is correct if both X and Y are either both classes or both interfaces.

The correct answer is Option C.