• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Abstract regular inner class

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The code shown here is from K&B chapter-8:Inner Classes, SelfTest Q No:12




from above example and the K&B book, i have understood some points about abstract regular inner classes, please verify whether is it correct or not.

1. If we make regular inner class as abstract then it is not necessary to make my outer class as abstract. It can be a normal class or an abstract class.

2. While making concrete subclass of my inner class either in same outer class or some other class, i shud have an instance of my outer class. For other classes, my outer class as well as my inner class shud be visible for them to make concrete subclass of my inner class.

3. Now concrete sub class of my innner class can access the instance variables of my outer class depending upon their visibility to concrete subclass.

4. If my concrete subclass of inner class is written in static method then this subclass cant access the instanse variables of my outer class using Outer.this reference.

Please correct if any of above my understanding is wrong.

Thanks in advance.

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kashif,

Point 1 : That is true. In general if a class is abstract , it can have concrete methods , inner class can be abstract , just that you cannot directly instantiate abstract classes.

Point 2 : Regular inner classes are like class members for the outer class. If you are instantiating the inner class in the same outer class then you need not use the explicit reference of the Outer class since you implicitly have "this" reference . But if you want to instantiate outside in some class then you gotta have the Outer class reference.

Point 3: Sub class of inner class is as good as a member of the Outer class . So it can access the instance variables of the Outer class.

Point 4 : Static methods cannot access instance variables / methods "directly". Method local inner classes cannot access the local variables of that method except those marked final. Method local inner classes can access the instance variables. But since the method is static , it cannot access the instance variables directly , you gotta have a reference to access the instance variables.

Hope this was of some help.

Note : Your questions are pretty confusing :-) you can probably frame them differently. KB explains Inner classes wonderfully.

~Aditya

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your POINT 1. IS WRONG!!
inner class are also like other members,so if inner class is abstract then the class having it must me ABSTRACT....
your point 3. is also dangerous so dont follow it blindly,
Go through the k&b inner class chapter once more its an awesome chapter.....!!
best of luck!!
 
Kashif Sayyed
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adi thanks for your reply and you are correct, my questions are a bit confusing. Actually those are just observations i got from playing with some inner class codes. So i just wanted to confirm them.

himanshu,
thanks for reply.
your answer for point 1 is wrong. Outer class can be normal or abstract if its inner class declared abstract.
And about point 3, it is not dengerous to directly use instance variables of outer class in concrete class of inner class because as adi said "Subclass of inner class is as good as a member of the Outer class . So it can access the instance variables of the Outer class. ".


thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic