Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract regular inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abstract regular inner class" Watch "Abstract regular inner class" New topic
Author

Abstract regular inner class

Kashif Sayyed
Greenhorn

Joined: Dec 18, 2007
Posts: 9
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.
Adi Kulkarni
Ranch Hand

Joined: Mar 12, 2009
Posts: 86
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


SCJP 1.5
himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
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!!


SCJP 5
Kashif Sayyed
Greenhorn

Joined: Dec 18, 2007
Posts: 9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract regular inner class