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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post 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.

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

Guest

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

Post 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.

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

pcoady
Posts: 1
Joined: Fri Mar 22, 2013 6:21 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

baichen7788
Posts: 23
Joined: Fri Mar 26, 2021 7:25 am
Contact:

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

Post by baichen7788 »

what do you mean by "hidden" ?

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

In what context? Where do you see hidden mentioned in this thread??

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests