Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confusion in inheritance - value of "this" when printed in constructor

I have the following code.

class Test {
    int i = 0;

    Test() {
        System.out.println(this);
        System.out.println(this.i);
    }

}

public class Demo extends Test {
    int i = 10;

    Demo() {
        super();
        System.out.println("calling super");
        System.out.println(this);
        System.out.println(this.i);    
    }

    public static void main(String[] args) throws IOException {    
        Demo d = new Demo();    
    }
}

O/P : Demo@2e6e1408
0
calling super
Demo@2e6e1408
10

When I execute the program and print the value of "this", in both super class constructor as well as in child class constructor, the value of this (address location) is displayed as childClassName@someValue .. My question is, why dont I get the value of Test i.e, Test@someVal (Super class) when I print value of "this" in the super class.. ASAIK, Super class will also have a place/location in memory, so, why am I not getting Test@someValue in the first SOP...

PS : I know variables are referenced based on the reference type (LHS) and methods are called based on the object type (RHS)..

like image 558
TheLostMind Avatar asked Dec 30 '13 06:12

TheLostMind


2 Answers

When I execute the program and print the value of "this", in both super class constructor as well as in child class constructor, the value of this (address location)...

The output of System.out.println(this) with the default Object#toString is not the address of the instance in memory. It's just the name of the class and the instance's hash code, nothing more. From the documentation:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

It's true that the hashCode documentation says

This is typically implemented by converting the internal address of the object into an integer...

but it also says

...but this implementation technique is not required by the JavaTM programming language.

...and of course the JVM is free to move instances around in memory as necessary, but isn't allowed to change the hashCode.


...why dont I get the value of Test i.e, Test@someVal (Super class) when I print value of "this" in the super class.

There is one instance. That instance is of the subclass. When you do System.out.println(this), it doesn't matter whether you do that in the base class or the subclass, it's still the same object instance you're using. That one object has features it gets from the subclass and also features it inherits from the superclass, but there aren't two separate instances; there's one instance with a combined set of features. super isn't an object reference, although it looks a bit like one; it's a syntax mechanism for specifically asking the compiler to use a feature the instance inherits from the superclass rather than the equivalent feature of the instance (in case they're different).

like image 82
T.J. Crowder Avatar answered Sep 17 '22 22:09

T.J. Crowder


Demo is a composition of 3 classes: itself, Test and Object. But an instance of Demo is one object, in memory it consists of fields of its super class + its own fields: Test.i + Demo.i (Object has no fields).

like image 45
Evgeniy Dorofeev Avatar answered Sep 20 '22 22:09

Evgeniy Dorofeev