2

I got two classes. A Superclass and a Subclass.

public class SuperClass
{

    protected int x;

    public SuperClass(){
        x=9;
    }

    public int getX(){
        return this.x;
    }

    public Object getObject(){
        return this;
    }
}


public class SubClass extends SuperClass
{
    private int x;

    public SubClass(){
        x = 1;
    }
}

If I now create a subclass

SubClass sub = new SubClass();

and call the two methods, "this" references in the first method the superclass in the second the SubClass itself

sub.getX();

returns

9

which is the value of x of the superclass

sub.getObject();

on the other hand returns a reference to SubClass.

What's the reason for the two different references of "this", or am I missing something?

This shouldn't be duplicate of Overriding member variables in Java ( Variable Hiding) cause I'm wondering why "this" alone references the subclass not why this.x is giving me the variable of the superclass

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
stef9998
  • 21
  • 3
  • 1
    The problem is that your subclass is hiding the super class' field `x`. When called on an instance of your subclass `this` will always refer to that instance and thus the type will be `SubClass`. `this.x`, however, returns the value of `SuperClass.x` because the compiler only sees that field in the super class. It would be the same as just `return x;` - the `this` doesn't change anything here. – Thomas Aug 30 '19 at 15:40
  • So an important question is, "why did you add `private x` to your Subclass?". Because depending on the answer to that question, you might be completely misunderstanding how visibility and inheritance works in java, which is good to sort out before you write more code. – Mike 'Pomax' Kamermans Aug 30 '19 at 15:43
  • 2
    `this` does not reference a _class_, it references the current _object_ you are in. – Seelenvirtuose Aug 30 '19 at 15:54
  • so only `this` is the dynamic type / the instance, and `this.x` is interpreted by the compiler directly so it's the static type. Am I understanding that right? – stef9998 Aug 30 '19 at 15:56
  • Make `x` in your sub class public, and see how the code behaves, this is the best way to test this mechanic. – Laf Aug 30 '19 at 15:58
  • 1
    making `x` public doesn't change anything as it shouldn't. – stef9998 Aug 30 '19 at 16:01
  • @stef9998 The two `this` references aren't different, they are the same. They reference the same (only) `SubClass` object you have created with `new`. When you remove the `private int x;` line you will change the `x` fields from the base class. – Progman Aug 30 '19 at 18:51
  • Your problem has very little to do with what 'this' refers to or the 'x' field in both classes having the same name (which is why indeed the qn shouldn't be marked duplicate even if the question title per se would strongly suggest so). The problem is that you are in the ***method*** 'getX' and that method exists only in the superclass. Therefore ***by definition*** the 'x' returned by that method must be the 'x' of the superclass (the compiler cannot possibly know about the possible existence of SubClass, and it could even be the case that there is no subclass at all) ... – Erwin Smout Oct 08 '19 at 10:55
  • ... If you were thinking of letting the compiler add run-time code to do the name resolution of the 'x' reference only at run time, think of the options there are if boh 'x'-es are not of the same type. – Erwin Smout Oct 08 '19 at 10:57

0 Answers0