J
Jonathan
Hi everyone,
I'm trying to understand inner classes, but came across the following
problem. If I define two types (one is an inner class), and each type
has a method with the same name, is there a way that I can call the
enclosing instance's method from within an instance of the inner class?
I'm guessing that (as per the example pasted below) the getValue()
methods aren't overloaded or overridden because the two classes don't
participate in super-class/sub-class OO relationship. I'd be grateful
for any insight.
Thanks,
Jonathan
public class EnclosingClass {
private int _value = 100;
private class InnerClass
{
public int getValue()
{
return _value * 2;
}
public void println()
{
System.out.println(getValue()); //<-- inner
System.out.println(getText()); //<-- enclosing
}
}
public int getValue()
{
return _value;
}
public String getText()
{
return "hello!";
}
public static void main(String[] args)
{
// create an instance of both classes and call the println method of
the inner class
new EnclosingClass().new InnerClass().println();
}
}
I'm trying to understand inner classes, but came across the following
problem. If I define two types (one is an inner class), and each type
has a method with the same name, is there a way that I can call the
enclosing instance's method from within an instance of the inner class?
I'm guessing that (as per the example pasted below) the getValue()
methods aren't overloaded or overridden because the two classes don't
participate in super-class/sub-class OO relationship. I'd be grateful
for any insight.
Thanks,
Jonathan
public class EnclosingClass {
private int _value = 100;
private class InnerClass
{
public int getValue()
{
return _value * 2;
}
public void println()
{
System.out.println(getValue()); //<-- inner
System.out.println(getText()); //<-- enclosing
}
}
public int getValue()
{
return _value;
}
public String getText()
{
return "hello!";
}
public static void main(String[] args)
{
// create an instance of both classes and call the println method of
the inner class
new EnclosingClass().new InnerClass().println();
}
}