Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Please explain this example. Its from Devaka's ExamLab

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Some one please explain me why I am getting output "HighHigh"?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Mansi Agarwal
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great explanation gurpeet! I hope not to encounter such a question in the exam ; - )

Regards,
Dan
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic