OCP Question 34, Explanation

Given:

Bread.java:

public class Bread {
    private String eat(String piece) { return "Consume " + piece; }
}

Pizza.java:

public class Pizza extends Bread {
    public String eat (String slice) { return "Enjoy " + slice; }
}

Test.java:

public class Test {
    public static void main (String[] args) {
        Bread b1 = new Bread();
        b1.eat("bread.");
        Bread b2 = new Pizza();
        b2.eat("Quattro Stagioni.");
    }
}

What is the result?

A. Consume bread.
Enjoy Quattro Stagioni.
B. Consume bread.
Consume Quattro Stagioni.
C. The Pizza.java file fails to compile.
D. The Test.java file fails to compile.

 

The correct answer is D.

The eat() method in the parent class Bread is private.

Leave Comment

Your email address will not be published.