18

Within a class, a field that has the same name as a field in the superclass hides the superclass's field.

public class Test {

    public static void main(String[] args) {

        Father father = new Son();
        System.out.println(father.i); //why 1?
        System.out.println(father.getI());  //2
        System.out.println(father.j);  //why 10?
        System.out.println(father.getJ()); //why 10?

        System.out.println();

        Son son = new Son();
        System.out.println(son.i);  //2 
        System.out.println(son.getI()); //2
        System.out.println(son.j); //20
        System.out.println(son.getJ()); //why 10?
    }  
}

class Son extends Father {

    int i = 2;
    int j = 20;

    @Override
    public int getI() {
        return i;
    }
}

class Father {

    int i = 1;
    int j = 10;

    public int getI() {
        return i;
    }

    public int getJ() {
        return j;
    }
}

Can someone explain the results for me?

3
  • 1
    Oracle Tutorial on hiding variables. Commented Sep 3, 2012 at 8:56
  • 2
    A son is not a type of father Commented Sep 3, 2012 at 9:13
  • 3
    I don't see how this question is "too localized"... :D Commented Nov 30, 2016 at 16:43

2 Answers 2

17

In Java, fields are not polymorphic.

Father father = new Son();
System.out.println(father.i); //why 1? Ans : reference is of type father, so 1 (fields are not polymorphic)
System.out.println(father.getI());  //2 : overridden method called
System.out.println(father.j);  //why 10? Ans : reference is of type father, so 2
System.out.println(father.getJ()); //why 10? there is not overridden getJ() method in Son class, so father.getJ() is called

System.out.println();

// same explanation as above for following 
Son son = new Son();
System.out.println(son.i);  //2 
System.out.println(son.getI()); //2
System.out.println(son.j); //20
System.out.println(son.getJ()); //why 10?
Sign up to request clarification or add additional context in comments.

1 Comment

Just in java? any other language provide polymorphism on fields?
4

As per Overriding and Hiding Methods

The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass.

i.e. when you invoke a method which is overridden in subclass via a super class reference the super class method is invoked and it access super class members.

This explains following as the reference used is of superclass:

System.out.println(father.i);  //why 1?
System.out.println(father.j);  //why 10?
System.out.println(father.getJ()); //why 10?

Similarly for the following:

System.out.println(son.getJ()); //why 10?

since getJ() is not defined in Son a Father version is invoked which sees member defined in the Father class.

If you read Hiding Fields; they specifically don't recommend such method of coding as

Generally speaking, we don't recommend hiding fields as it makes code difficult to read.

2 Comments

I am sorry, but this is not true. The original source has been misquoted. Your quotation is specifically in reference to static methods, not general methods. Please note the original source wording, as follows. (1) "The version of the overridden instance method that gets invoked is the one in the subclass." (2) "The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass."
So, in essence, if you invoke a method that is overridden in the subclass, via the superclass reference, the SUBCLASS method version is invoked... NOT the superclass method as you implied.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.