Parent and Child Classes Having Same Data Member in Java Last Updated : 13 May, 2022 Comments Improve Suggest changes Like Article Like Report In C++ we have all class member methods as non-virtual. In order to make so, we have to use a keyword as a prefix known as virtual. Whereas in Java, we have all class member methods as virtual by default. In order to make them non-virtual, we use the keyword final. Reference variables in Java are basically variables holding the address of the object in hexadecimal type which later is converted to the binary system that basically is the address of the object to be stored on the heap memory. Reference variables that differ from primitive types as their size can not be calculated. In Java, the reference variable of the Parent class is capable to hold its object reference as well as its child object reference. Let's see about non-method members with the help of an example. Example: Java // Java Program to Demonstrate that Non-method Members // are Accessed according to Reference Type // (Unlike methods that are accessed according // to the referred object) // Class 1 // Super class class Parent { int value = 1000; // Constructor of super class Parent() { // Print statement System.out.println("Parent Constructor"); } } // Class 2 // Sub class class Child extends Parent { int value = 10; // Constructor of sub class Child() { // Print statement System.out.println("Child Constructor"); } } // Class 3 // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of sub class inside main() // method Child obj = new Child(); // Printing the reference of child type 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 // Printing the reference of parent type System.out.println("Reference of Parent Type : " + par.value); } } OutputParent Constructor Child Constructor Reference of Child Type :10 Reference of Parent Type : 1000 Output Explanation: 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 the compiler uses a special run-time polymorphism mechanism only for methods. (There the type of object being instantiated decides which version of the method to be called). Note: It is made possible to access child data members using parent pointer with typecasting. Comment More infoAdvertise with us Next Article Classes and Objects in Java T twinkle tyagi Improve Article Tags : Java Practice Tags : Java Similar Reads Accessing Protected Members in Java In Java, there are four types of access modifiers. These are public, private, default, and protected. To get the idea of these modifiers, you can refer to access modifiers in java. In this article, we discuss the accessibility of protected members in different cases. Now let us discuss various scena 6 min read Classes and Objects in Java In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. A class is a template to create objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects.An 10 min read Classes and Objects in Java In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. A class is a template to create objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects.An 10 min read Protected vs Final Access Modifier in Java Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi 5 min read Understanding Classes and Objects in Java The term Object-Oriented explains the concept of organizing the software as a combination of different types of objects that incorporate both data and behavior. Hence, Object-oriented programming(OOPs) is a programming model, that simplifies software development and maintenance by providing some rul 10 min read Understanding Classes and Objects in Java The term Object-Oriented explains the concept of organizing the software as a combination of different types of objects that incorporate both data and behavior. Hence, Object-oriented programming(OOPs) is a programming model, that simplifies software development and maintenance by providing some rul 10 min read Like