Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium

Question 56

Question ID: UKOCP53245


Consider below codes of 3 java files:




Which of the following needs to be done so that there is no compilation error?

  • A No need for any modifications, code compiles as is
  • B

    Replace /*INSERT*/ with below code:

  • C

    Replace /*INSERT*/ with below code:

  • D

    Replace /*INSERT*/ with below code:

  • E

    Replace /*INSERT*/ with below code:

  • F

    Replace /*INSERT*/ with below code:

Reveal correct answer

Correct answer: F

Explanation

UKOCP53245:

Profit class causes compilation error as it complains about duplicate default methods: Profitable1.profit() and Profitable2.profit(). To rectify this error abstract class Profit must override the profit() method.


default keyword for method is allowed only inside the interface and default methods are implicitly public. So overriding method should use public modifier and shouldn't use default keyword.

If you want to invoke the default method implementation from the overriding method, then the correct syntax is: [Interface_name].super.[default_method_name].

Hence, Profitable1.super.profit(); will invoke the default method of Profitable1 interface and Profitable2.super.profit(); will invoke the default method of Profitable2 interface.


Based on above points, let's check all the options one by one:

No need for any modifications, code compiles as is: ✗


Replace /*INSERT*/ with below code:

✗  profit() method must be declared with public access modifier.


Replace /*INSERT*/ with below code:

✗  default keyword for method is allowed only inside the interface.


Replace /*INSERT*/ with below code:

✗  profit() method must be declared with public access modifier.


Replace /*INSERT*/ with below code:

✗  Profitable1.profit(); causes compilation error as correct syntax is: Profitable1.super.profit();


Replace /*INSERT*/ with below code:

✓  It compiles successfully.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need