• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

inner_classes

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class tester{
tester caller(boolean b){

class inner_c extends tester{
int i = 5;
void printval(){
System.out.println("initialized inner i " + i);
}
}
inner_c c = new inner_c();
return c;
}
public static void main(String args[]){
tester t = new tester();
tester temp = t.caller(false);
temp.printval();
}
}

Can you pl tell me how to access the method printval() which is defined in the inner class inner_c ?? The above line 'temp.printval()' gives an error???
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class inner_c is part of caller( ) rather than being part of tester. (Also notice that you could use the class identifier inner_c for an inner class inside each class in the same subdirectory without a name clash.) Therefore, inner_c cannot be accessed outside of caller( ). Notice the upcasting that occurs in the return statement�nothing comes out of caller( ) except a reference to tester, the base class. Of course, the fact that the name of the class inner_c is placed inside caller( ) doesn�t mean that inner_c is not a valid object once caller( ) returns but you can't access it. All I want to say here is that the inner class if defined in a method is local to that method like any other members of the method that's why you can't access the inner class inside a method or any of its members from outside.
Please read Chapter 8 of Thinking in Java for details about inner classes.

Originally posted by vishal p:
class tester{
tester caller(boolean b){

class inner_c extends tester{
int i = 5;
void printval(){
System.out.println("initialized inner i " + i);
}
}
inner_c c = new inner_c();
return c;
}
public static void main(String args[]){
tester t = new tester();
tester temp = t.caller(false);
temp.printval();
}
}

Can you pl tell me how to access the method printval() which is defined in the inner class inner_c ?? The above line 'temp.printval()' gives an error???



[This message has been edited by manali (edited August 22, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider this class tester{
tester caller(boolean b){
class inner_c extends tester{
int i = 5;

void printval(){
System.out.println("initialized inner i " + i);
}

}
inner_c c = new inner_c();
if(c instanceof tester)
System.out.println("c instanceof tester");
return c;
}
public static void main(String args[]){
tester t = new tester();
tester temp = t.caller(false);
//if (temp instanceof inner_c)
// System.out.println("temp instanceof inner_c ");
// temp.printval();
}
}
output c instanceof tester
and put this in
if (temp instanceof inner_c)
System.out.println("temp instanceof inner_c ");
compie error
javac tester.java
tester.java:25: Class inner_c not found.
if (temp instanceof inner_c)
^
1 error
outside the caller( ), inner_c are invisible
Mick
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vishal,
You can access the method printval() defined inside the inner class inner_c, when you define printval() in the outerclass also. In this case dynamic binding and polymorphism takes place.
so if your code is as follows...
class tester{
tester caller(boolean b){
class inner_c extends tester{
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 tester1.printval()");
}
public static void main(String args[]){
tester t = new tester();
tester temp = t.caller(false);
temp.printval();
}
}
Hope this helps...
Vani.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vani,
I'd like you to explain in more detail why the code works when defining the method void printval() in class tester.
IMHO, class inner_c is la "local innner class" and it is NOT accesible outside the method where is defined.
The method tester caller(boolean b) is returning a reference to the local inner class just created, whose definition is extending the outer class, so when the line
tester temp = t.caller(false); is executed, conversion up the inheritance hierarchy is automatically provided.
Until this point, I understand the code.
But, when I get missed is when the line temp.printval(); is done. IMHO, the object's class referred by temp is "inner_c", and the method called would be the one in this class(means late binding).... so the output is....
But, what's the reason to have the same method void printval() defined in the outer class. I try to do without it, and obtained the following compiler error:
Tester.java:21: Method printval() not found in class Tester.

Could anybody throw some light into this?
Thanks in advance.
Regards.
�ngel C.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like there is some confusion here. Let me try to blow the dust out My explanation below basically supplements Vani's arguments and her 'corrected' class code.
This code deals with two concepts - overriding and inner classes. Lets get these concetps straightened out first.
overriding - The class inner_c extends the class tester and overrides the method printval(). Everything here is legal.
Innerclass - - The class inner_c is an inner class of class tester. Angel, note that it is not defined in a method scope, but it is defined in a class scope. We all know to create an instance of this kind of inner class, we need a reference to the outer class. Since the inner_c instance is being created in the method tester.caller the outer instance 'this' serves as the enclosing context for the inner_c instance 'c' being created. Everything here is legal too.
So far so good.
Now lets look at the main method.
Line 1 : tester t = new tester() ;
This creates an object of type tester and assigns it to a reference of type tester. Perfectly valid "plain vanilla " line of code.
Line 2 : tester temp = t.caller(false);
This calls the method on the tester object. Note the type of the object being returned is inner_c, the reference it is being assigned to is tester. We know this is possible( and permitted ) according to reference conversion rules.
Line 3: temp.printval();
The final truth!.
Here temp is of type inner_c hence run time polymorphism kicks in. It calls the printval() method on the inner_c instance which prints "initizlized inner i 5". The emphasis here is on the type of object temp is pointing to, and not its declared type.

I hope things are clear now.
Ajith
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ajith
It was a thorough explanation and very educating one. I appreciate ur time to help us out.
I'd a couple of ques on this one.
1. is it that because reference variable temp is of type tester, the compiler looks for the method printval() in the class tester.
2. can we use a reference variable of type inner_c here in the main method? if so, how? In that case, maybe we don't need to define the printval() method in the tester class.
Thanks.
[This message has been edited by Harry Chawla (edited August 26, 2000).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic