About Question enthuware.ocajp.i.v7.2.970 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.970 :
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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.970 :
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.
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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970 :
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.
BTW, Using an IDE sometimes clouds the things. So we recommend using notepad and command line while preparing for this exam.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Fri Mar 22, 2013 6:21 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970 :
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?
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?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970 :
Yes! Fields are shadowed and methods are overridden.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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Mar 26, 2021 7:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970 :
what do you mean by "hidden" ?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970 :
In what context? Where do you see hidden mentioned in this thread??
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 46 guests