2

I have Parent Class and Child Class like this:

class Parent {

    public String value = "Parent";

    public String getValue() {

        return value;
    }
}

class Child extends Parent {

    public String value = "Child";

    public String getValue() {
        return value;
    }

}

public class Test {

    public static void main(String[] args) {

    Parent abc = new Child();       

    System.out.println(abc.value +"  | "+abc.getValue());


    }
}

If I run the code the result is Parent | Child. Why is like that? Why field 'value' is from Parent class and method is from Child, not both from Child?

Gregory
  • 65
  • 6
  • 2
    Because methods are polymorphic, and fields aren't, basically. There's no idea of "overriding" a field. – Jon Skeet Jan 31 '18 at 11:44
  • 1
    polymorphism isn't applied to fields, only for instance methods – Andrew Tobilko Jan 31 '18 at 11:44
  • 1
    At runtime for fields:- referenced type is checked and for methods:- if overriden, child class method is called. Similar question asked here:- https://stackoverflow.com/questions/10722110/overriding-member-variables-in-java – mrinal Jan 31 '18 at 11:51

3 Answers3

1

The reason beign that you overridden the getValue() method. You can't override fields.

The type of the reference abc is important in the compile time, during runtime the type of object that actual value of abc points to determines which method will be invoked. Because abc is a reference to the instance of Child class, the method most apropriate for the Child object will be invoked.

Yoda
  • 17,363
  • 67
  • 204
  • 344
1

In Java it is not possible to override fields. The field is bound to the Reference type of the object , in this case it is Parent. Hence, you are seeing the value of Parent class's variable.

However, It is possible to override method and the same is reflected in your code for getValue() method. Hence you are seeing the method output of the actual object bound at runtime, i.e. Child class.

Priya Jain
  • 795
  • 5
  • 15
1

Java Language was designed without the functionality to override fields, but allowing you to override methods - this is the main reason why you can't get polymorphical effect with the fields. Polymorphism is taken into account when you have overriden methods in child class (with exactly the same signature and return type). As fields doesn't support polymorphism, field from declared class is taken:

Parent abc = new Child(); // declared type is Parent 

You may want to have a look at this link (https://docs.oracle.com/javase/tutorial/java/IandI/override.html) to read a bit more about overriding methods:

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method. The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type.

Additional informations about when polymorphism is taken into account, you may find here:

https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21