Java SE 8 Programmer II Exam Question: Inheritance and Interfaces

Java SE 8 Programmer II Exam Question: Inheritance and Interfaces

Question

Given: public class Canvas implements Drawable { public void draw (){ } } public abstract class Board extends Canvas {} public class Paper extends Canvas { protected void draw (int color){} } public class Frame extends Canvas implements Drawable { public void resize (){ } } publicinterface Drawable { public abstract void draw (); } Which statement is true?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D. E.

E.

The given code snippet defines several classes and interfaces in Java. Let's examine each of them one by one and see whether they compile or not.

  1. Canvas class:

The Canvas class implements the Drawable interface and provides an implementation for its draw() method. Since the draw() method is abstract in the Drawable interface, the Canvas class is required to provide an implementation for it. Therefore, the Canvas class compiles successfully.

  1. Board class:

The Board class is an abstract class that extends the Canvas class. Since the Canvas class already provides an implementation for the draw() method of the Drawable interface, the Board class is not required to provide an implementation for it. Therefore, the Board class compiles successfully.

  1. Paper class:

The Paper class extends the Canvas class and provides an implementation for its own draw(int color) method. This method is not related to the draw() method of the Drawable interface, so it is not required to be declared as public or abstract. Since there is no issue with the implementation of the Paper class, it compiles successfully.

  1. Frame class:

The Frame class extends the Canvas class and implements the Drawable interface. It also provides its own resize() method. Since the Canvas class already provides an implementation for the draw() method of the Drawable interface, the Frame class is not required to provide an implementation for it. Therefore, the Frame class compiles successfully.

  1. Drawable interface:

The Drawable interface declares a single draw() method, which is required to be implemented by any class that implements it. Since there is no issue with the declaration of the Drawable interface, it compiles successfully.

Therefore, the correct answer is option E: all classes compile successfully.