• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

method overriding

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am taking a practice exam and don't understand the answer to the following question:

Consider the following code:



What is the result?

A. Car
B. Vehicle
C. Compiler error at line 3
D. Compiler error at line 5
E. Exception thrown at runtime

I thought the answer was D, but apparently I'm wrong....it's A.

Why can class PE14 override the run() method if it is declared as private in class Vehicle?

Thanks.

-Jason
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason,
Well buddy the answer to your question is the run() method is not inherited by PE14 class so there is no question of overriding happening !!! The PE14 class doesnt even know that Vehicle class has a run() method because it is marked private in the Vehicle class and private members cannot be seen even by subclasses. So the "new PE14().run();" statement just executes the run() method in the PE14 class.. Simple as that. Hope this helps answering your question. Work harder on access modifiers...

Regards,
Greg
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that was a real cool eye catcher by the way. it almost got me ! nice one buddy
 
jason roberts
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Makes sense now...Thanks alot!
reply
    Bookmark Topic Watch Topic
  • New Topic