• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Which one is the main calling?

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic