2

let's say i have class A and class B which extends A, here are the classes:

A:

public class A {
    public int x;
    public static int y;
    public A(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int getX() { return x; }
    public static int getY() { return y; }
    public A get1() { return this; }
    public A get2() { return new B(x, y); }
}

B:

public class B extends A {
    public int x;
    public B(int x, int y) {
        super(x, y);
        this.x = x*2;
        this.y = y*2;
    }
    public int getX() { return x; }
        public static int getY() { return y*3; }
    public A get1() {
        x++;
        return super.get1();
    }
    public A get2() { return get1(); }
}

Here is the main function:

public static void main(String[] args) {
    A a1 = new A(5, 10);
    A a2 = a1.get2();
    A a3 = a2.get2();

    System.out.println("a1.x=" + a1.x);
    System.out.println("a1.y=" + a1.y);
    System.out.println("a2.x=" + a2.x);
    System.out.println("a2.getX()=" + a2.getX());
    System.out.println("a2.getY()=" + a2.getY());
    System.out.println("((B)a2).getY()=" + ((B)a2).getY());
    System.out.println("((B)a2).x=" + ((B)a2).x);
    System.out.println("a3 is A: " + (a3.getClass() == A.class));
    System.out.println("a3 is B: " + (a3 instanceof B));
    System.out.println("a3==a2: " + (a3 == a2));
}

My problem is with the a2 and a3 objects, a3 is basically a2.get2(), after following the method it will reach the A get1() method that returns this.

Since the method is found in class A i was sure it will only return the reference to A part of object a2 and not a reference to the whole object,

So when i try this line: a3.getClass() == A.class i will get True.

When i debugged a3.getClass() is "class B".

Can someone explain to me what the return this line actually does when it's inside a father class?

Thanks!

argamanza
  • 1,122
  • 3
  • 14
  • 36
  • 2
    What is `a2` and `a3`? What you you mean by `a3` is basically `a2.get2()`. What do you mean by after following the method. Please provide all relevant information. – Chetan Kinger Jun 20 '15 at 14:04
  • Well, so forget the "i was sure it will only return the reference to `A` part". There are no references to part of an object in Java. – laune Jun 20 '15 at 14:04
  • And please don't post statements such as "i will get `True`" without corroberating code. – laune Jun 20 '15 at 14:12
  • @ChetanKinger i updated the post with the relevant main function which for some reason i forgot in the initial post. – argamanza Jun 20 '15 at 14:28
  • Wow, I am surprised at the fact that you left out the main method and assumed that people would just read your mind. – Chetan Kinger Jun 20 '15 at 14:29
  • @ChetanKinger again i'm sorry but i didn't assume that, it was left out by mistake, notice i referenced the object names from the main program so i was going to paste it later, and forgot. – argamanza Jun 20 '15 at 14:32
  • 1
    @argamanza My bad. It could have been an accident. See my answer for a step by step explanation. – Chetan Kinger Jun 20 '15 at 14:42

2 Answers2

2

Let's trace the statements step by step :

  1. a1 is a reference to an instance of type A.
  2. a1.get2() calls the get2() method in A which returns a reference to an instance of type B so a2 refers to an instance of type B.
  3. a2.get2() calls the get2() method in B. Remember that a2 is an instance of type B so this refers to B.
  4. get2() method in B calls the get1() method in B. this still refers to B.
  5. get1() method in B calls super.get1(). This is where it can get a bit confusing. Even though you call the get1 method from the parent class, this still refers to B at runtime.
  6. Therefore, super.get1() returns B. get1() in B returns B. get2() in B returns B. Therefore, a3 refers to an instance of type B.

From the java docs for Object#getClass

public final Class getClass()

Returns the runtime class of this Object

The getClass method returns the runtime class of an object so that's what you get when you call getClass on an a reference to an instance of type B. If getClass was not designed to return the actual instance type, it would have always returned Object which would make the method pointless.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
1

The keyword this refers to the current object instance. There is no "A part of the B object", i.e., there is no reference to a superclass from within a subclass. Objects that are inherited are not divided into their different parts; you instantiate one object, and it is referred to by this from within instance methods, regardless of where those instance methods are declared.

So you have a B object, and this within a method declared in A. If the method is called, directly or indirectly, from a B, then it is going to refer to that B object.

arcy
  • 12,845
  • 12
  • 58
  • 103