Mansi Agarwal wrote:Thanks Henry.
Here is what I understand from it: As runNow() is private, and B is a static inner class(which has no special relationship with the outer class) it is not considered to be overridden. So the runNow() is selected according to the refernce type instead of object type.
Is it right?
static inner class(which has no special relationship with the outer class) it is not considered to be overridden.
classes are not overridden, only methods are. it has nothing to do with static inner class. yes static class object does not have reference to enclosing class object, but the main point to understand here is that the runNow method in class A is private. no doubt static class B extends A , but keep in mind this class B HAS NO KNOWLEDGE that there is runNow() method in its parent class i.e. A. it has no idea , it is completely unaware that there is runNow method. hence B class cannot inherit runNow() method. since there is no inheritance of runNow() method, there is no overriding. the runNow() method defined in B class is completely new method. just by luck B class has a method with the same name runNow(). so when you encounter A aa = new B(); aa.runNow(), compiler knows that aa has runNow() method, but it is for sure that any implementing class of A(here B) cannot inherit runNow() method(hence no overriding), so it calls runNow method from A class and prints High.
the second case is bit more tricky. class C extends class B and OVERRIDES its runNow() method. keep in mind class C OVERRIDES runNow() method of B class and NOT of A class(which is private and nobody except class A has the knowledge that it exists). so when you do A aa = new C(); aa.runNow() , compiler knows for sure that A has runNow() method and in no case C inherits the same method and hence no overriding. so it simply runs runNow method of A class and prints High.
if you change the access modifier of class A to public and then run the program you will get output as Low Out, because of overriding
did you get it ? if still not you can post your doubts here.