Return to site

☕🎓JAVA CERTIFICATION QUESTION: private local variable type inference

· ocp,java

Given the following interface and two classes:

and a method fragment

Which statement is true? Choose one.

* To make the method compilable, you must change the code as follows:

03: House.Floor floor = house.getFloor();

* To make the method compilable, you must change the inner class access modifier as follows:

public class Floor implements Paintable

* To make the method compilable, you must change the code as follows:

05: itemsToPaint.add((Paintable) floor);

* The method compiles without changes.

#java #certificationquestion #ocp

Answer: The method compiles without changes.

The code declares a private inner class, Floor, within the House class.

Because the class is private, the type prevents arbitrary usage outside the House class.

However, this type is also returned by the public method getFloor().

This raises the question of how the caller of that method (in line 03 of the method fragment) will see that returned type.

House.Floor floor = house.getFloor(); suggests declaring the floor variable on line 03 with the type House.Floor, but this is the private type.

You should know that in code outside the House class, this approach will not compile.

In the end, the code compiles, so the correct answer is "The method compiles without changes."