wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confused with output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused with output" Watch "Confused with output" New topic

Confused with output

Bhushan Kumar

Joined: Jan 28, 2002
Posts: 7

The out put should be 9 Accelerate : SportsCar, but When i run the above code it is printing
8 Accelerate : SportsCar, Can some body please clarify me on this?
Please use the [ code ] opening and closing tags (without the spaces) when posting source code.
- satya
[ February 09, 2002: Message edited by: Madhav Lakkapragada ]
Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
Ok this is easy. The way this works is as follows:
In java the variable values are selected at compile time and the object is dynamicaly chosen(ie at run time).
In th case the variable value selected would be 8 as it is at compile time and the the method that would be run would belong to SportsCar as it is chosen at run time. I hope thai amkses it clear. Let me know if you have any more questions.
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
Fields and methods behave differently between classes and their subclasses.
Methods can be overriden, but fields are hidden.
When you make a method call with a reference variable, as when you write c.accelerate() , the VM will make a polymorphic method call; that is, at RUN TIME, it will deterime what method to call based on the actual class instance that the variable c refers to. This is dynamic binding.
However, fields don't work this way. When you make a reference to a field, as when you wrote c.gearRatio, it is determined at COMPILE time which gearRatio field will be accessed. In this case, the type of c determines which gearRatio field. Since c is defined to be of type Car, the compiler inserts code at compile time to access the gearRatio field of the Car class. At runtime, the fact that the object refered to by c is really a subclass of Car is not at all a factor. This is an example of static, or early binding. Static methods also behave in this manner.
Just remember that ONLY instance methods are subject to polymorphic calls; all other members (fields, static methods) are subject to static binding at compile time.

SCJP 1.4
Bhushan Kumar

Joined: Jan 28, 2002
Posts: 7
Thanks a lot to Rob & Hemal. I am cleared.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Your name doesn't agree with the javaranch guidelines. Please take a moment and edit your profile after reviewing the guidelines at http://www.javaranch.com/name.jsp
Please change your Publicly Displayed name to follow the guidelines.
thanks for your cooperation
- satya

Take a Minute, Donate an Hour, Change a Life
Bhushan Kumar

Joined: Jan 28, 2002
Posts: 7
Hi Madhav
I did Edit my profile.
Erik Dark
Ranch Hand

Joined: Jan 28, 2002
Posts: 107
Exactly the same question here.
Erik Dark
I agree. Here's the link: http://aspose.com/file-tools
subject: Confused with output
It's not a secret anymore!