Variables in Java Do Not Follow Polymorphism and Overriding Last Updated : 23 Sep, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Variables in Java do not follow polymorphism. Overriding is only applicable to methods but not to variables. In Java, if the child and parent class both have a variable with the same name, Child class's variable hides the parent class's variable, even if their types are different. This concept is known as Variable Hiding. In the case of method overriding, overriding methods completely replace the inherited methods but in variable hiding, the child class hides the inherited variables instead of replacing them which basically means that the object of Child class contains both variables but Child's variable hides Parent's variable. Hence when we try to access the variable within Child class, it will be accessed from the child class. If we are trying to access the variable outside of the Parent and Child class, then the instance variable is chosen from the reference type. Example Java // Java Program Illustrating Instance variables // Can not be Overridden // Class 1 // Parent class // Helper class class Parent { // Declaring instance variable by name `a` int a = 10; public void print() { System.out.println("inside B superclass"); } } // Class 2 // Child class // Helper class class Child extends Parent { // Hiding Parent class's variable `a` by defining a // variable in child class with same name. int a = 20; // Method defined inside child class public void print() { // Print statement System.out.println("inside C subclass"); } } // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating a Parent class object // Reference Parent Parent obj = new Child(); // Calling print() method over object created obj.print(); // Printing superclass variable value 10 System.out.println(obj.a); // Creating a Child class object // Reference Child Child obj2 = new Child(); // Printing childclass variable value 20 System.out.println(obj2.a); } } Outputinside C subclass 10 20 Conclusion: Variables in Java do not follow polymorphism and overriding. If we are trying to access the variable outside of Parent and Child class, then the instance variable is chosen from the reference type. Comment More infoAdvertise with us Next Article Dynamic Method Dispatch or Runtime Polymorphism in Java D dattabikash505 Follow Improve Article Tags : Java Blogathon Blogathon-2021 java-inheritance java-overriding +1 More Practice Tags : Java Similar Reads Dynamic Method Dispatch or Runtime Polymorphism in Java Prerequisite: Overriding in java, Inheritance Method overriding is one of the ways in which Java supports Runtime Polymorphism. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. When an overridden method is called thro 5 min read Compile Time Polymorphism in Java Polymorphism in Java refers to an object's capacity to take several forms. Polymorphism allows us to perform the same action in multiple ways in Java. Polymorphism is divided into two types: Compile-time polymorphismRun time polymorphismNote: Run time polymorphism is implemented through Method overr 7 min read Can we override private methods in Java? Let us first consider the following Java program as a simple example of Overriding or Runtime Polymorphism. Java class Base { public void fun() { System.out.println("Base fun"); } } class Derived extends Base { public void fun() { // overrides the Base's fun() System.out.println(" 4 min read How to overload and override main method in Java How to overload main method in java? Method Overloading can be defined as a feature in which a class can have more than one method having the same name if and only if they differ by number of parameters or the type of parameters or both, then they may or may not have same return type. Method overloa 4 min read Can We Override Default Method in Java? Default method in Java is a method in java which are defined inside the interface with the keyword default is known as the default method. It is a type of non-abstract method. This method is capable of adding backward capability so that the old interface can grasp the lambda expression capability. J 3 min read Overriding toString() Method in Java Java being object-oriented only deals with classes and objects so do if we do require any computation we use the help of object/s corresponding to the class. It is the most frequent method of Java been used to get a string representation of an object. Now you must be wondering that till now they wer 3 min read Like