aspose file tools*
The moose likes Beginning Java and the fly likes private inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "private inner classes" Watch "private inner classes" New topic
Author

private inner classes

Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

I'm a bit confused with regard to private inner classes.

Here's the assignment:

Create a private inner class that implements a public interface. Write a method that returns a reference to an instance of the private inner class, upcast to the interface. Show that the inner class is completely hidden by trying to downcast to it.


And here's what I've got, and it works:



LivingRoom is a private inner class, yet the outer class has access to its constructor? I thought it was supposed to be completely hidden. Of course it it were, there wouldn't be a way to do anything with the inner class (I think), and we can at least instantiate an object. I am a bit confused though. Any thoughts?



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

Paul Mrozik wrote:I'm a bit confused with regard to private inner classes.

Here's the assignment:

Create a private inner class that implements a public interface. Write a method that returns a reference to an instance of the private inner class, upcast to the interface. Show that the inner class is completely hidden by trying to downcast to it.


And here's what I've got, and it works:



LivingRoom is a private inner class, yet the outer class has access to its constructor? I thought it was supposed to be completely hidden. Of course it it were, there wouldn't be a way to do anything with the inner class (I think), and we can at least instantiate an object. I am a bit confused though. Any thoughts?


The inner class, declared as private in the outer class, is private to the outer class. Just like variable and methods, declared as private in the outer class, is private to the outer class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
My interpretation would be that they want you to create a separate class, that calls a method from the first class. That method's return type will be the interface, and the method will return an instance of the private inner class. In the outside class, they want you to try to downcast the return object you get from the method to show that the other class doesn't see the private inner class.
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

The inner class, declared as private in the outer class, is private to the outer class. Just like variable and methods, declared as private in the outer class, is private to the outer class.

Henry


I read what you said a couple of times, and I'm not sure what you mean. Fields declared as private, as such:



...are accessible. Outer has access to Inner.j and Inner has access to Outer.i.

My beef is with the constructor of Inner if Inner had been declared private.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

You cannot create an instance of an inner class without first having an instance of the outer class.

If you take that rule away then nothing can create an instance of the inner class.
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Okay so I did this:



And no problems, but doing what you said, Keith:



Gave me this:


Outer2.java:6: cannot find symbol
symbol : class Inner
location: class Outer2
Inner n = k.new Inner();
^
Outer2.java:6: Outer.Inner has private access in Outer
Inner n = k.new Inner();


So I think I get it now.

Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

Paul Mrozik wrote:
The inner class, declared as private in the outer class, is private to the outer class. Just like variable and methods, declared as private in the outer class, is private to the outer class.

Henry


I read what you said a couple of times, and I'm not sure what you mean. Fields declared as private, as such:



...are accessible. Outer has access to Inner.j and Inner has access to Outer.i.

My beef is with the constructor of Inner if Inner had been declared private.


Please read your original post again, there was no mention of this. You simply asked about the class declaration -- not the constructor of the inner class declaration, which BTW, is declared as public !!

The ability of an outer class accessing private members of an inner class is a different question ... and the answer is related to this...


Section 6.6.1 of the Java Language Specification wrote:A private class member or constructor is accessible only within the body of the top level class (ยง7.6) that encloses the declaration of the member or constructor.


The scope of private is for the body of the top level class. This means that a outer class can access the private members of its inner class, the inner class call access the private members of its outer class. And even, an inner class can access the private members of another inner class within the same outer class. Think of "private" as being accessible to everything within the body of the top level outer class.

Henry
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Thanks for you reply Henry, and I apologize for answering so late. I only get a chance to work with Java on the weekends.

So here's what I said in my original post:

LivingRoom is a private inner class, yet the outer class has access to its constructor? I thought it was supposed to be completely hidden.


So I did ask about the constructor of the inner class.

And what you posted:

The scope of private is for the body of the top level class. This means that a outer class can access the private members of its inner class, the inner class call access the private members of its outer class. And even, an inner class can access the private members of another inner class within the same outer class. Think of "private" as being accessible to everything within the body of the top level outer class.



Thanks, I now understand how it all works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private inner classes
 
Similar Threads
inner classes
[to - Eric]..inner class
Doubt in Access modifiers for classes
anonymous inner class and Constructor
Anonymous class implementing Interface