Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Same field name in superclass and subclass

956015Mar 24 2013 — edited Mar 24 2013
Hi,

I'd like to know what the effect is when you have two fields of the same name in a superclass and subclass. Something along the lines of:
class Base
{
   int x = 0;
}

class Derived extends Base
{
   int x = 1;
}
If I store a reference to a Derived object in a Base reference variable called base and I do base.x I get 0.
If I store the reference in a Derived reference variable called der and I do der.x I get 1.
Are there actually two fields with name x in instances of Derived?

Thank you.

Edited by: 953012 on Mar 24, 2013 4:32 PM

Edited by: 953012 on Mar 24, 2013 4:33 PM
This post has been answered by unknown-7404 on Mar 24 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 21 2013
Added on Mar 24 2013
2 comments
1,624 views