0

I'm confused because I thought this referred to the current object calling on the method.
So why didn't the instance variable x in my object not get changed when calling on an inherited method? Superclass:

public class SuperBoss 
{
  int x = 50;

  public void changeX()
  {
   this.x  = 20;
  }
}

Subclass:

public class Boss extends SuperBoss
{
 int x = 10;
 public static void main(String[] args)
 {
  Boss b = new Boss();
  b.changeX();
  System.out.println(b.x); //prints 10
 }
}

Why does it print 10 and not 20?

1
  • Because java supports behavior overriding and not property overriding. In other words. Java supports method overriding and not variable overriding. Commented Apr 16, 2013 at 4:14

2 Answers 2

5

Short answer: Because field accesses are not virtual in Java.


SuperBoss declares x.

When Boss declares x, x does not become a 'virtual' field - it becomes a new field that is hidden from the superclass's field.

When you call changeX on your Boss, which is a method in SuperBoss, SuperBoss doesn't know about Boss.x and accessing x is not virtual, so it just accesses SuperBoss.x.

If you need accesses to x to be virtual, you need to provide a getX method, and override the getX method in Boss. Now when SuperBoss's methods call getX, it gets rerouted to the getX in Boss.

Sign up to request clarification or add additional context in comments.

8 Comments

omg I accidentally hit edit on your answer! Damn I hope it doesn't edit it. I mean to comment back saying thanks for answering. What do you mean by "virtual" ?
@KacyRaye - You did not edit it... There would be a note to the left of his name that would say "Last edited at...".
@Kacy Raye 'virtual' means that if a subclass provides a new definition and you have an instance of the subclass, any call to the method is rerouted to the new definition even in the superclass(es). Non-virtual means that superclass usage would not be redirected to the new definition.
Awesome, and just to be sure I'm totally clear on what you're saying, when you say "new definition," you are referring to the fields right?
@Kacy Raye Yes. I think it's also possible to declare new methods in Java in a way such that they are non-virtual, e.g., calls in the superclass won't be rerouted to the subclass's definition in a subclass.
|
1

When you declare int x = 10 in your subclass you're hiding the superclass variable from everyone when they're looking at the subclass.

When the superclass calls this.x it's not looking at the subclass, so it's allowed to get it's own version of the variable.

Variables are not actually stored by they're code name at runtime, so redefining them like this doesn't work - SuperBoss#x will resolve to one symbol in the symbol table and Boss#x will resolve to another, so they both still exist, if you know how to get at them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.