4

I am studying for my OCA exam and came across this problem and It is very confusing.

Variable resolution always based on reference type instead of the runtime object. So if my parent and child class look like this:

class Parent {

    public int x = 1;

    public int getX() {
        return x;
    }
}

class Child extends Parent {
    public int x = 2;

    public int getX() {
        return x;
    }
}

Based on the rules (variable resolution is always based on reference type), the following code is behaving as expected:

    Child c = new Child();
    System.out.println(c.x);                    //2
    System.out.println(((Parent) c).x);         //1

However. if I retrieve the variables by using the getter defined in the parent and child class, then I get this:

    Child c = new Child();
    System.out.println(c.getX());               //2
    System.out.println(((Parent) c).getX());    //2

Shouldn't it print the same as if I was to access the variable directly? why would getting the variable via getter be different than getting the variable directly?

One theory I have is this:

Since instance method resolution is based on runtime object, therefore it overrides the variable resolution rule. In other words, the compiler will resolve the getter methods to the child class since instance method resolution is based on runtime object. Am I right?

Thank you

0

4 Answers 4

4

Methods and variables behave differently. Variables do not get inherited or participate in polymorphism.

The Parent and Child classes each have their own distinct variable x. One doesn't override the other. You can use the cast to specify that you want the Parent's x, not the Child's.

With the method, it doesn't matter what cast you stick on it, when calling the Child you get the overridden version of getX. (Within a method of the Child you can call super.getX to get the parent's version of the method.)

TLDR: variable resolution rules don't apply to methods. Polmorphism doesn't apply to variables.

0
System.out.println(((Parent) c).getX());    //2

This dispatches to Child.getX() because of polymorphism. Then the variable resolution rules apply in the context of this method. They aren't overridden. So yes, you are essentially correct.

0

Polymorphism is a mechanism applied before calling a method, which always looks for the most specific override in the whole chain of subclasses, regardless of the instance's declared type.

There is no such mechanism for fields. Giving the two variables the same name "x" does not connect them in any way, so giving those two fields the same name will likely only serve to confuse future readers of your code.

0

Variable resolution is based on the reference type. The reference is of parent type so it will get the value of x of the parent class.

while; The Method resolution is based on the runtime object. The runtime object is of a child class so the method resolution with getX() will get the value of x of the child class.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.