Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

inner class/inheritance

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic