Click the Exhibit button. 1. public interface A { 2. public void doSomething(String thing); 3. } 1. public class AImpl implements A { 2. public void doSomething(String msg) { } 3. } 1. public class B { 2. public A doit() { 3. // more code here 4. } 5. 6. public String execute() { 7. // more code here 8. } 9. } 1. public class C extends B { 2. public AImpl doit() { 3. // more code here 4. } 5. 6. public Object execute() { 7. // more code here 8. } 9. } Which statement is true about the classes and interfaces in the exhibit?

  1. Compilation will succeed for all classes and interfaces.

  2. Compilation of class C will fail because of an error in line 2.

  3. Compilation of class C will fail because of an error in line 6.

  4. Compilation of class AImpl will fail because of an error in line 2.


Correct Option: C
Explanation:

To solve this question, users need to understand the concept of interfaces and inheritance in Java programming language.

  • The interface A declares a method called doSomething that takes a string parameter.
  • The class AImpl implements the A interface and provides an implementation for the doSomething method.
  • The class B declares a method called doit that returns an object of type A.
  • The class C extends B and declares a method called doit that returns an object of type AImpl. It also declares a method called execute that overrides the execute method in class B and returns an object of type Object.

Now let's go through each option:

A. Compilation will succeed for all classes and interfaces.

This option is incorrect. Although there are no syntax errors in the code, there is a logical error in class C. It overrides the doit method in class B with a method that returns AImpl instead of A. This violates the Liskov Substitution Principle, which states that subclasses should be substitutable for their base classes. As a result, compilation will fail for class C.

B. Compilation of class C will fail because of an error in line 2.

This option is incorrect. There are no syntax errors in line 2 of class C. The error is a logical error, as explained above.

C. Compilation of class C will fail because of an error in line 6.

This option is correct. The execute method in class C attempts to override the execute method in class B. However, the return type of the method in class C is Object, which is not a subtype of the return type of the method in class B, which is String. As a result, compilation will fail for class C.

D. Compilation of class AImpl will fail because of an error in line 2.

This option is incorrect. There are no syntax errors in line 2 of class AImpl.

Therefore, the correct answer is:

The Answer is: C. Compilation of class C will fail because of an error in line 6.

Find more quizzes: