Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Which one is the main calling?

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

What will be printed when SportsCar is run?
Answer :8 Acelerate : SportsCar
I feel it should print :-
9 Acelerate : SportsCar, since in the main you are saying "new SportsCar()"
Please explain?
Sonir
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sonir shah:

What will be printed when SportsCar is run?
Answer :8 Acelerate : SportsCar
I feel it should print :-
9 Acelerate : SportsCar, since in the main you are saying "new SportsCar()"
Please explain?
Sonir



The varibles are called by reference type and not by object type. Here the refrence is of type Car so Car.gearRatio will be printed
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When accessing fields, the compile-time type counts (here c.gearRatio will be the gearRatio of class Car), while when accessing methods the run-time type is taken into account (you got that one )
HIH
 
We don't have time for this. We've gotta save the moon! Or check this out:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic