2

I know Variables doesn't exhibit polymorphic behavior and its limited to methods. But below code is bit confusing in returning the type of object.

Its invoking the child method (which is right) and that means it is returning the child object. So why not printing the child variable's value?

class Parent {

    int var = 11;

    public Parent getInstance() {
        System.out.println("In Parent ...");
        return new Parent();
    }
}

class Child extends Parent {

    int var = 22;

    public Child getInstance() {
        System.out.println("In Child ...");
        return new Child();
    }
}


public class VariableHiding {

    public static void main(String[] args) {
        Parent p = new Child();
        System.out.println(p.getInstance().var);  // something's fishy?
    }
}


// Output : In Child ...  11

PS : Its not the duplicate of this one (which talks about variable not taking part in polymorphism, but the one mentioned here returns the object of child obj and that obj holds var of parent instead of its own)

Snehal Masne
  • 3,403
  • 3
  • 31
  • 51

3 Answers3

1

Runtime polymorphism cannot be achieved for variables. So both the class Parent and Child have a common variable var.

Parent p = new Child();

In this p, referred by class Child(which is of type of class Parent).So it will always refer to super class variable.

NivedhaLak
  • 199
  • 1
  • 1
  • 10
0

The return type of p.getInstance() is Parent, since p's compile-time type is Parent, and the getInstance() method of Parent returns a Parent.

You can see that if you try to write:

Parent p = new Child();
Child c = p.getInstance();

which won't pass compilation.

Therefore p.getInstance().var returns the instance variable of Parent class.

In order to get the instance variable of Child class, you'll have to cast p.getInstance() to Child:

System.out.println(((Child)p.getInstance()).var);

or change the type of p to Child:

Child p = new Child();
System.out.println(p.getInstance().var);
Eran
  • 387,369
  • 54
  • 702
  • 768
0

The output is 11 instead of 22 because with line

 Parent p=new Child();

has been declared a reference to Parent, not to Child. This is legal ( Child is a Parent ) but from the point of view of the variable var the object even if costructed as a Child is considered a Parent, so the value written in Child is not considered.
The story would be completely different if the variable var was defined within the costructors, in the following way

 class Parent {

    protected int var;

    Parent(){ var=11;}

    public Parent getInstance() {
        System.out.println("In Parent ...");
        return new Parent();
    }
}

class Child extends Parent {

    Child(){ var=22;}

    public Child getInstance() {
        System.out.println("In Child ...");
        return new Child();
    }
}

This time using the same main function the result is 22,as desidered, because the value 11 defined in Parent's costuctor is overriden by the value 22 defined in the Child's one.

Jul10
  • 503
  • 7
  • 19