Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium
Question 56
Question ID: UKOCP53245
Consider below codes of 3 java files:
- //Profitable1.java
- package com.udayankhattry.ocp;
- public interface Profitable1 {
- default double profit() {
- return 12.5;
- }
- }
- //Profitable2.java
- package com.udayankhattry.ocp;
- public interface Profitable2 {
- default double profit() {
- return 25.5;
- }
- }
- //Profit.java
- package com.udayankhattry.ocp;
- public abstract class Profit implements Profitable1, Profitable2 {
- /*INSERT*/
- }
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:
- double profit() {
- return 50.0;
- }
-
C
Replace /*INSERT*/ with below code:
- public default double profit() {
- return 50.0;
- }
-
D
Replace /*INSERT*/ with below code:
- protected double profit() {
- return 50.0;
- }
-
E
Replace /*INSERT*/ with below code:
- public double profit() {
- return Profitable1.profit();
- }
-
F
Replace /*INSERT*/ with below code:
- public double profit() {
- return Profitable2.super.profit();
- }
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:
- double profit() {
- return 50.0;
- }
✗ profit() method must be declared with public access modifier.
Replace /*INSERT*/ with below code:
- public default double profit() {
- return 50.0;
- }
✗ default keyword for method is allowed only inside the interface.
Replace /*INSERT*/ with below code:
- protected double profit() {
- return 50.0;
- }
✗ profit() method must be declared with public access modifier.
Replace /*INSERT*/ with below code:
- public double profit() {
- return Profitable1.profit();
- }
✗ Profitable1.profit(); causes compilation error as correct syntax is: Profitable1.super.profit();
Replace /*INSERT*/ with below code:
- public double profit() {
- return Profitable2.super.profit();
- }
✓ It compiles successfully.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
