Variables in Java Do Not Follow Polymorphism and Overriding


In the world of Object-Oriented Programming (OOP), polymorphism and overriding are key concepts that lend flexibility and dynamism to programming languages. Java, being a robust OOP language, fully supports these features. However, it's crucial to understand that these characteristics apply to methods in Java and not to variables. In this article, we will explore why variables in Java do not follow polymorphism and overriding, providing a deeper understanding of Java's variable behavior.

Polymorphism in Java

Polymorphism, a Greek word meaning "many forms," is a fundamental concept of OOP. It allows objects of different types to be treated as objects of a common supertype, making it possible to write more flexible and reusable code. In Java, polymorphism is implemented through inheritance, interfaces, and method overriding.

Example

Let's examine an example

class Animal {
   void sound() {
     System.out.println("The animal makes a sound");
   }
}
class Cat extends Animal {
   @Override
   void sound() {
      System.out.println("The cat meows");
   }
}
public class Main {
   public static void main(String[] args) {
      Animal myAnimal = new Cat();
      myAnimal.sound();
   }
}

Output

The cat meows

In this scenario, myAnimal is an Animal reference variable pointing to a Cat object. When the sound() method is invoked on myAnimal, the version in the Cat class is called, not the one in the Animal class. This is polymorphism in action, where the method to be invoked is determined by the actual object type and not by the reference type.

Overriding in Java

Method overriding is a specific form of polymorphism in Java, where a subclass provides a specific implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass.

Why Variables Don't Follow Polymorphism and Overriding?

Why Variables Don't Follow Polymorphism and Overriding? Unlike methods, variables in Java do not follow the concepts of polymorphism and overriding. This discrepancy arises from the fundamental difference in how methods and variables exist and operate in an object

In Java, instance variables belong to an instance of a class, which means each instance has its own copy of an instance variable. Therefore, changing the value of an instance variable in one object does not affect other objects of the same class.

On the other hand, methods belong to the class itself and not to any specific instance. This means that methods, unlike variables, do not have separate copies for each object of the class.

Example

Let's revisit the previous example but add instance variables this time:

class Animal {
   String sound = "The animal makes a sound";
   void makeSound() {
      System.out.println(sound);
   }
}
class Cat extends Animal {
   String sound = "The cat meows";
   @Override
   void makeSound() {
      System.out.println(sound);
   }
}
public class Main {
   public static void main(String[] args) {
      Animal myAnimal = new Cat();
      myAnimal.makeSound();
      System.out.println(myAnimal.sound);
   }
}

Output

The cat meows
The animal makes a sound

In this code, myAnimal is an Animal reference pointing to a Cat object. The makeSound() method call on myAnimal will print "The cat meows," but the System.out.println(myAnimal.sound) line will print "The animal makes a sound". Why does this happen? Because methods follow polymorphism, the makeSound() method from the Cat class is executed. However, since variables don't follow polymorphism, the `sound variable from the Animal class is used

This behavior is due to a principle called variable hiding. If a variable in a subclass has the same name as one in its superclass, the subclass variable hides the superclass variable

This doesn't mean the superclass variable has been overridden. Both variables still exist independently, and the one that gets used is determined by the reference type, not by the actual object type. This is why, when we access the sound variable through the Animal reference, we get the sound value from the Animal class, not the Cat class.

Variable Overriding vs Variable Hiding

In Java, variables are not subject to overriding like methods. Instead, they follow the principle of variable hiding. Variable hiding and method overriding are fundamentally different:

  • Method Overriding − In overriding, a subclass provides a different implementation for a method that is already defined in its superclass. The decision of which method to invoke is based on the actual object's type, not the reference type, which allows for polymorphism.

  • Variable Hiding − In variable hiding, if a variable in a subclass has the same name as one in its superclass, the subclass variable hides the superclass variable. The decision of which variable to use is based on the reference type, not the actual object's type.

These principles stem from the fact that methods are behavior representations, while variables represent state. The behavior can be polymorphic and overridden, allowing objects of different types to behave differently. In contrast, the state, represented by variables, belongs to a specific instance and isn't polymorphic.

Conclusion

In summary, understanding why variables in Java do not follow polymorphism and overriding offers vital insight into Java's working principles. This knowledge is crucial for Java programmers to avoid misunderstandings and bugs when working with inheritance and polymorphism

Updated on: 19-Jul-2023

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements