This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
A signature always reveals a man's character - and sometimes even his name.
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.
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!!
Joined: Dec 18, 2007
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.
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. ".