Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic