• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class/inheritance

 
Dan Andrei
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Question:
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 ]
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Obj1.new Obj2() IS WHEN CLASS Obj3 HAS ALSO A
CLASS Obj2, which hides the Obj2 class in Obj1 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic