Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Parent and Child classes having same data member in Java

  • Difficulty Level : Medium
  • Last Updated : 10 May, 2021

The reference variable of the Parent class is capable to hold its object reference as well as its child object reference.
In Java, methods are virtual by default (See this for details).
What about non-method members. For example, predict the output of following Java program.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.


// A Java program to demonstrate that non-method
// members are accessed according to reference
// type (Unlike methods which are accessed according
// to the referred object)
class Parent
    int value = 1000;
        System.out.println("Parent Constructor");
class Child extends Parent
    int value = 10;
        System.out.println("Child Constructor");
// Driver class
class Test
    public static void main(String[] args)
        Child obj=new Child();
        System.out.println("Reference of Child Type :"
                           + obj.value);
        // Note that doing "Parent par = new Child()"
        // would produce same result
        Parent par = obj;
        // Par holding obj will access the value
        // variable of parent class
        System.out.println("Reference of Parent Type : "
                           + par.value);


Parent Constructor
Child Constructor
Reference of Child Type : 10
Reference of Parent Type : 1000

If a parent reference variable is holding the reference of the child class and we have the “value” variable in both the parent and child class, it will refer to the parent class “value” variable, whether it is holding child class object reference. The reference holding the child class object reference will not be able to access the members (functions or variables) of the child class. This is because the parent reference variable can only access fields that are in the parent class. Thus the type of reference variable decides which version of “value” will be called and not the type of object being instantiated. It is because compiler uses special run-time polymorphism mechanism only for methods. (There the type of object being instantiated decides which version of the method to be called).
It is possible to access child data members using parent pointer with typecasting. Please see last example of this for complete code.
This article is contributed by Twinkle Tyagi .If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!