• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Devaka Coorey Exam Lab

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from Diagnostic Test from Exam Lab -



Output is - High,High

I though the output will be Low,Out as run time Polymorphism will come into picture and runNow method in B & C would have been called respectively.

Thanks in advance.
 
author
Posts: 23919
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

The trick to this question is actually very well hidden -- it could fool anyone... Luckily, the actual test will probably not be as mean...

Hint: Take a look at the class A's method. What access level does it have? And how does that access level affect inheritance?

Henry
 
Sushant Kaushik
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops...I missed that...but still in Class B we can assume that runNow() method is overrideen even though its private in class A, isn't it??
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class B and C is not really overriding the runNow() method of class A because it has the private access modifier, hence, it is invisible in the class B and C. And you can see in the code that the declared type of variable "a" is of type A. And this variable was declared inside class A that's why it will call its own private runNow() method which is only visible within class A.
 
Sushant Kaushik
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it...Thanks Henry & Alex..
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But inner class does have the access to private members of the enclosing class then why not it overridding it?
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

geeta vemula wrote:But inner class does have the access to private members of the enclosing class then why not it overridding it?



private members are never overridden...
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As compiler made to know only one thing that polymorphism does not apply to private member, after all your private things are private, you do not want to share it with others.
 
Ankit Garg
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lemme give you an example code

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone help me? I don't understand the syntax.

What does it mean? A[] a=new B[]{new B(),new C()};

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

Tom Mark wrote:Can anyone help me? I don't understand the syntax.

What does it mean? A[] a=new B[]{new B(),new C()};



See comments below...



The trick to this question lied here:





Pay special close attention to codes on Exam and break down and analyze codes line by line to make sure you're not get tricked.






 
Tom Mark
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic