• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private inner classes

 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
Chrome Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
Chrome Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
Chrome Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic