• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Are fields never overriden?

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across this problem while testing out covariant return types



The output to the above is

Returning Car
Returning CarInVehicle
vehicleCar 3
vehicleCar2 3



I was suprised to see 3 on both occassions. I was expecting 5 for both because i knew that at runtime the JVM will look at the actual object but this doesnt seem to be the case.

Does this mean that methods can be overriden but fields work the same way as overloading a method? I cant find this described anywhere in the book.

Thanks

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this.
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ogeh. That example is slightly different as it is showing an example of shadowing i.e. overriding a variable within the same class using local variables. I was refering to overriding a variable via inheritance.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:Thanks Ogeh. That example is slightly different as it is showing an example of shadowing i.e. overriding a variable within the same class using local variables. I was refering to overriding a variable via inheritance.


It's a similar principle, though. Only (non-static) methods are ever overridden.

(If you follow good practice and make instance variables private, this should never cause any confusion as the variables aren't visible to subclasses anyway)
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldnt normally code it like that but was just testing to see what would happen.
Thanks you both for your time.
 
straws are for suckers. tiny ads are for attractive people.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic