OCP Question 34, Explanation



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


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


public class Test {
    public static void main (String[] args) {
        Bread b1 = new 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.