Quiz your self: How non-public is a Java non-public inside class?

Given the next interface and two courses

Quiz Yourself, Oracle Java Certified, Oracle Java Certification, Oracle Java Career, Oracle Java Skill, Oracle Java Jobs, Oracle Java Prep, Oracle Java Preparation

interface Paintable {

  void paint(String colour);


class Home implements Paintable {

  non-public class Flooring implements Paintable {

    public void paint(String colour) {

      System.out.println(“Flooring painted to ” + colour);



  Flooring f;

  public void paint(String colour) {

    System.out.println(“Home painted to ” + colour);


  public Home() {f = new Flooring();  }

  public Flooring getFloor() {return f;}


01: var itemsToPaint = new ArrayList<Paintable>();

02: var home = new Home();

03: var ground = home.getFloor();

04: itemsToPaint.add(home); 

05: itemsToPaint.add(ground);

06: itemsToPaint.stream().forEach(p -> {

07:     p.paint(“PINK”);

08: });

Which assertion is true? Select one.

A. To make the strategy compilable, you should change the code as follows:

     03: Home.Flooring ground = home.getFloor();

B. To make the strategy compilable, you should change the inside class entry modifier as follows:

     public class Flooring implements Paintable

C. To make the strategy compilable, you should change the code as follows:

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

D. The strategy compiles with out adjustments.

Reply. This query investigates some subtlety within the that means of personal because it pertains to Java varieties. The code within the query declares a personal inside class, Flooring, inside the Home class. As a result of the category is non-public, the kind prevents arbitrary utilization outdoors the Home class. Nevertheless, this kind can be the kind returned by the general public methodology getFloor(). This raises the query of how the caller of that methodology (in line 03 of the strategy fragment) will see that returned sort.

Possibility A suggests declaring the ground variable on line 03 with the kind Home.Flooring, however that is the non-public sort. You need to know that in code outdoors the Home class, this method is not going to compile. From this you may shortly reject possibility A as incorrect.

Choices B and C each counsel that you should change the code—in different phrases, they each assert that the code doesn’t compile because it stands. It seems that these choices are each incorrect: The code does in truth compile as written. Subsequently, possibility D is right.

There are 3 ways the variable ground on line 03 might be declared to permit that line of code to be compiled. Every declaration has barely totally different penalties.

The primary approach. Any reference sort might be saved in a variable of sort Object, and the identical is true at line 03. Should you do that, you’d be capable to execute the essential strategies of Object on the reference variable ground. This isn’t very useful, nonetheless, because the variable wouldn’t be a legitimate argument for the add methodology on line 05, which expects a Paintable argument.

The second approach. You may make ground of sort Paintable. If each the Paintable interface and the code within the methodology fragment are in the identical package deal, the compiler will allow you to retailer the returned worth of getFloor in a variable of this kind. As a result of the compiler is aware of that the Home.Flooring sort is Paintable, and that interface is accessible to the code, the compiler is completely satisfied. Doing this additionally permits the ground variable to be a legitimate argument to the add methodology on line 05. Clearly that is preferable to utilizing Object for the kind of the ground variable and would enable the code to work—however as famous, the change isn’t vital, as a result of the instance code is already right.

The third approach. The compiler will even mean you can declare ground utilizing the var pseudotype. This, after all, is what the code already does. So, what occurs once you do that? The compiler ascribes the variable as a so-called nondenotable artificial sort. The impact is that the strategy fragment is aware of nearly nothing concerning the object to which ground refers. You possibly can’t invoke the paint methodology on it and, in truth, you may’t even invoke strategies of Object (equivalent to equals or toString) on it—that’s certainly a shock! Nevertheless, the compiler retains observe of the truth that this object refers to an occasion of Home.Flooring, and should you cross this object right into a name for any methodology for which that’s a legitimate argument sort, the receiving methodology invocation shall be legitimate.

In a way, the compiler says “OK, methodology fragment: I do know what this object is, though you aren’t allowed to know. Should you accurately cross the article to one thing else, I’ll deal with that, however you may’t do anything with this.”

Which means which you could name the add methodology on line 05 as a result of that wants a Paintable, and the compiler is aware of that the Home.Flooring sort implements Paintable. As well as, should you had an accessible methodology outlined in Home that took a Home.Flooring argument, you would additionally name that with ground as an argument. However, as famous, the strategy fragment can’t name any methodology on the ground reference.

With all that mentioned, the underside line is that the code works as written, and no adjustments are wanted.

This dialogue is a simplification of the complete story and is an try to clarify the sensible facet of the habits. In order for you all of the nitty-gritty particulars, enumerated as details somewhat than as explanations, you may learn extra within the 4 references listed within the “Dig deeper” part under.

As a facet observe, this query nearly definitely goes past the depth of the examination, so contemplate this an attention-grabbing puzzler and perception into Java, somewhat than an genuine instance of an examination query.

Conclusion. The right reply is possibility D.

Supply: oracle.com

(Visited 4 times, 1 visits today)

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
Would love your thoughts, please comment.x