aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain this example. Its from Devaka's ExamLab Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain this example. Its from Devaka Watch "Please explain this example. Its from Devaka New topic
Author

Please explain this example. Its from Devaka's ExamLab

Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53


Some one please explain me why I am getting output "HighHigh"?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Mansi Agarwal wrote:

Some one please explain me why I am getting output "HighHigh"?



Hint: From the Java language specification, regarding the rules of overriding a method.... http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.1

specifically, this requirement ....

m2 is public, protected, or declared with default access in the same package as C, or


Notice that private methods are *not* on the list.

Henry



Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mansi Agarwal
Ranch Hand

Joined: Apr 11, 2012
Posts: 53
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?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Great explanation gurpeet! I hope not to encounter such a question in the exam ; - )

Regards,
Dan

William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Thanks Dan. yeah it was tricky and under exam pressure one could have commit mistake. but if we calmly go through it , you will find that it wouldn't be that hard.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Please explain this example. Its from Devaka's ExamLab