124k views
3 votes
If an inner class calls a method defined in the outer class AND in the inner class, which will execute?

a) Outer class method
b) Inner class method
c) Both outer and inner class methods
d) Compiler error

2 Answers

4 votes
In this case, if an inner class calls a method that is defined in both the outer class and the inner class, the inner class method will execute. This is because the inner class has its own method with the same name, and it takes precedence over the method in the outer class.
User Twaddington
by
8.4k points
6 votes

Final answer:

The method in the inner class will execute when called from within that inner class because it has precedence over the outer class method in scope.

Step-by-step explanation:

When an inner class calls a method that is defined in both the outer class and the inner class, the method that will execute is the one in the inner class. This is because when the method is called from within the inner class, the scope of the inner class takes precedence, and its method overrides the one in the outer class. If the intention is to call the outer class method, specific syntax such as OuterClass.this.methodName() can be used.

User Nsgocev
by
8.6k points