Final answer:
In Java, an inner class can call a method of the outer class with the same name using the syntax 'OuterClass.this.methodName()'. This differentiates the outer class method from the one in the inner class.
Step-by-step explanation:
In Java, if an inner class has a method with the same name as a method in its outer class, it can still call the outer class's method using the OuterClass.this.methodName() syntax. This tells the compiler explicitly which method to call, ensuring that the method from the outer class is executed rather than the method from the inner class. Here's an example:
Let's say we have an outer class named Outer and an inner class named Inner:
public class Outer {
public void show() {
// Method in the outer class
}
class Inner {
public void show() {
// Method in the inner class with the same name
}
public void callOuterShow() {
Outer.this.show(); // Calling the outer class's method
}
}
}
In the above code snippet, the callOuterShow() method in the inner class calls the show() method of the outer class, despite having its own show() method. To do this, it uses Outer.this.show().