File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class/inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner class/inheritance" Watch "inner class/inheritance" New topic

inner class/inheritance

Dan Andrei
Ranch Hand

Joined: Jan 21, 2004
Posts: 92

When I compile the above its OK
at point (***1) I instatiate an instanance of the Obj2 class inherited in Obj3
at point (***3) I do the same thing and I can “drop” the outter class instance since the this is passed implicitly to the method. I can also drop the qualifing Obj3.Obj2 since I’m in a method of Obj3 so I use just Obj2
At point 2 I do NOT understand why the line Obj2 oo2=new Obj1().new Obj2()
its ok !!! I think it should be Obj1.Obj2 oo2=new Obj1().new Obj2(); since the variable instantiated it’s the inner class of Obj1, and we are in a method of Obj3 ???
I notice that if Obj3 does not extend Obj1 I get a compile error for the above, but I do not understand how inheritance can make the line compile, after all we are dealing with an object that does not belong to this class: new Obj1().new Obj2();
Edited by Corey McGlone: Added CODE Tags and Reformatted Code for readability.
[ April 22, 2004: Message edited by: Corey McGlone ]

"Did anyone understand what I have just explained? ... because I did not!"
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Obj3 extends Obj1, which defines the inner class Obj2. So, Obj3 inherits the definition of that inner class Obj2. All three of those lines are possible due to the scope of the function. "foo" is inside Obj3, so it has a direct access to the definition of Obj2. That is why you don't have to fully specify the type as you did for line 1.

Dan Andrei
Ranch Hand

Joined: Jan 21, 2004
Posts: 92
thanx for the reply, I had a "hunch" that was the reason , but I needed a confirmation.
One more thing:
SO the only time I really need to use an
aceess like Obj1.Obj2=new Obj2() IS WHEN CLASS Obj3 HAS ALSO A
CLASS Obj2, which hides the Obj2 class in Obj1 ?
I agree. Here's the link:
subject: inner class/inheritance
jQuery in Action, 3rd edition