Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 230

Given:

public final class IceCream {

public void prepare() {}

}

public class Cake {

public final void bake(int min, int temp) {}

public void mix() {}

}

public class Shop {

private Cake c = new Cake ();

private final double discount = 0.25;

public void makeReady () { c.bake(10, 120); }

}

public class Bread extends Cake {

public void bake(int minutes, int temperature) {}

public void addToppings() {}

}

Which statement is true?

IceCream.

    Correct Answer: C

    In the provided code, the class Bread attempts to override the method bake(int min, int temp) from the class Cake, which is declared as final. In Java, a final method cannot be overridden by subclasses. Therefore, trying to override the final method in the Bread class will result in a compilation error. This makes the statement 'A compilation error occurs in Bread' the correct choice.

Discussion
adnano1234Option: C

The answer is C: Final method cannot be overridden, it could be only overloaded

asdfjhfgjuaDCVOption: C

Compilation error occurs in Bread:Answer is C Compile Errors : ./Main.java:14: error: bake(int,int) in Bread cannot override bake(int,int) in Cake public void bake(int minutes, int temperature) {} ^ overridden method is final

iSnoverOption: C

The answer is C, Final method cannot be overridden, it could be only overloaded.

steefaandOption: C

C is correct, as final method can't be overridden.