Page 1 of 1

About Question enthuware.ocajp.i.v7.2.970 :

Posted: Thu Sep 27, 2012 5:33 am
by ETS User
I don't understand how 8 will be printed. Since the object is of type SportsCar, the variable c.gearRatio will refer to the member field gearRatio of SportsCar.

Re: About Question enthuware.ocajp.i.v7.2.970 :

Posted: Thu Sep 27, 2012 7:59 am
by admin
This is because unlike instance methods, instance fields are shadowed and not overridden. Section 3.4.5 of the following document should be helpful: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_04.htm

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.970 :

Posted: Thu Dec 13, 2012 9:26 pm
by Guest
Can you explain why when I copy and paste the code into eclipse and run the program why I get "9 Accelerate : SportsCar" ?

I was under the impression (from other learning material) that it should only print 8 if gearRatio was declared as a static member i.e. public static int gearRatio = 8, otherwise it should use the instance member.

Re: About Question enthuware.ocajp.i.v7.2.970 :

Posted: Fri Dec 14, 2012 6:23 am
by admin
Can you please paste the exact code that you ran so that I can see what's going on?

BTW, Using an IDE sometimes clouds the things. So we recommend using notepad and command line while preparing for this exam.
-Paul.

Re: About Question enthuware.ocajp.i.v7.2.970 :

Posted: Fri Mar 22, 2013 6:31 pm
by pcoady
I got, 8 Accelerate : SportsCar when c was declared as Car c = new SportsCar();
but when I changed declaration to SportsCar c = new SportsCar(); I got 9 Accelerate : SportsCar
Is this because the fields accessed depends on the reference type?

Re: About Question enthuware.ocajp.i.v7.2.970 :

Posted: Fri Mar 22, 2013 6:33 pm
by admin
pcoady wrote:I got, 8 Accelerate : SportsCar when c was declared as Car c = new SportsCar();
but when I changed declaration to SportsCar c = new SportsCar(); I got 9 Accelerate : SportsCar
Is this because the fields accessed depends on the reference type?
Yes! Fields are shadowed and methods are overridden.

Re: About Question enthuware.ocajp.i.v7.2.970 :

Posted: Sat Jun 05, 2021 9:45 pm
by baichen7788
what do you mean by "hidden" ?

Re: About Question enthuware.ocajp.i.v7.2.970 :

Posted: Sat Jun 05, 2021 11:26 pm
by admin
In what context? Where do you see hidden mentioned in this thread??