aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner class" Watch "inner class" New topic
Author

inner class

Rishi Wright
Ranch Hand

Joined: Jun 25, 2003
Posts: 46
Hi 'LL..
I came across this code while searching for inner classes in javaranch.
class Test{
Test caller(boolean b){
class inner_c extends Test{
int i = 5;
void printval(){
System.out.println("initialized inner i " + i);
}
}
inner_c c = new inner_c();
return c;
}
void printval()
{
System.out.println("Inside Test1.printval()");
}
public static void main(String args[]){
Test t = new Test();
Test temp = t.caller(false);
temp.printval();
}
}
as per my knowledge inner_c is an local class defined in a method caller.
then temp is able to access printval() in inner_c(Not In Test class) as it is out of scope in the main methiod
thnx
rishi
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Rishi
I don't really know whether I understood your question fully or not. Well here's my take at it.
Test caller(boolean b) returns a Test type of object which in this case is an instance of class inner_c.
So basically you can replace

with

[ August 05, 2003: Message edited by: Anupam Sinha ]
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi Rishi,
The class inner_c, although a local class, can extend the Test class. So when it defines the method printval(), it overrides Test's printval() method. And when the call temp.printval() is made, polymorphism will still be at work. The method to invoked will be resolved a run-time, and in this case, it will resolve to inner_c's printval().
Hope this helps.
[ August 05, 2003: Message edited by: Alton Hernandez ]
Rishi Wright
Ranch Hand

Joined: Jun 25, 2003
Posts: 46
Ok Alton ..that means if u extend any outer class(or any class outside that method )then local class will be still available as a subclass even we exit the method..is it so?? is this same for anonymus class also?
thnx
rishi
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi Rishi,
A little modification on the code will show you that this is also true for anonymous class:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner class
 
Similar Threads
explanation needed
Basic Java Question
static vs non-static
inner_classes
Use of constructor in abstract class?