Java - Variable and Method Hiding Example

If Java, if you are not careful you can possibly hide both methods and variables of the superclass. A field or variable is said to hide all fields with the same name in superclasses. Similarly, a static method with the same name in a subclass can hide the method of the superclass. This can lead to subtle bugs, especially if you are expecting a different method to be called. In this article, I'll show you examples of both variable and method hiding in Java so that you can understand and avoid them in the future.


1. Method Hiding

Method hiding is closely related to method overriding and sometimes programmer hides the method trying to override it. The concept of overriding allows you to write code which behaves differently depending upon an object at runtime.

As I said, a static method with the same name in a subclass can hide the method from superclass because a static method cannot be overridden in Java.

Here is an example of method hiding in Java:


class Parent {
 
  public static void sleep() {
    System.out.println("Sleeps at 11 PM");
  }
}
 
class Child extends Parent {
 
  public static void sleep() {
    System.out.println("Sleeps at 9 PM");
  }
}
 
public class Code {
 
  public static void main(String[] args) {
    Parent p = new Parent();
    Parent c = new Child();
    p.sleep();
    c.sleep();
 
  }
}
 
Output:
Sleeps at 11 PM
Sleeps at 11 PM

In this example, we assume that p.sleep() will call the sleep() method from Parent class and c.sleep() will call the sleep() method from child class, just like it happens in overriding but because sleep() is a static method, instead of overriding we have hidden the sleep() method.

Since static method are resolved at compile-time, both are p.sleep() and c.sleep() are resolved to sleep() method of Parent class because the type of p and c variable is Parent. If you remove the static keyword from both methods then it will behave as expected.

Btw, in Java, you cannot override a static method as an instance method or vice-versa. So, if you remove static keyword from either subclass or superclass method you will get a compile-time error as "This instance method cannot override the static method from Parent" or "This static method cannot hide the instance method from Parent".

In short, a static method with the same name and signature in a subclass can hide a static method of the superclass. You can further join The Complete Java Masterclass to learn more about the static method in Java. It's one of the most up-to-date course and covers Java 12.

Java - Variable and Method Hiding Example



2. Variable Hiding

A field or variable with the same name in subclass and superclass is known as variable hiding or field hiding in Java. When a variable is hidden, you can use super.variableName to access the value from a superclass.

Here is an example of variable hiding in Java:

public class Parent {
  int age = 30;
}
 
class Child extends Parent {
  int age = 4;
 
  public void age() {
    System.out.println("Parent's age: " + super.age);
    System.out.println("Child's age: " + age);
  }
}

As you can see, you can access the superclass value using super.age.


That's all about method and variable hiding in Java. I don't encourage you to use the same name in both superclass and subclass unless you are overriding. This behavior can result in confusing code and cause subtle issues. Instead, try to avoid name class and provide a more meaningful name.

Further Learning
The Complete Java Masterclass
Java Fundamentals: The Java Language
Data Structures and Algorithms: Deep Dive Using Java


Other Java Overloading and Overriding Articles you may like
  1. Can you overload a static method in Java?(answer)
  2. Can you override a static method in Java? (answer)
  3. Why Java doesn't support operator overloading? (answer)
  4. Can you overload or override main method in Java? (answer)
  5. Rules of method overriding in Java? (answer)
  6. Difference between overriding, overloading, shadowing and obscuring in Java? (answer)
  7. What is Polymorphism? Method Overloading or Overriding? (answer)
  8. 19 Method overloading and overriding interview questions? (list)
  9. Constructor and Method Overloading best practices in Java (best practices)
  10. Difference between method overloading and overriding? (answer)
  11. What is the real use of overloading and overriding? (answer)
  12. What is Constructor overloading in Java? (answer)

Thanks for reading this article for so far. If you like this method and variable hiding article then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

No comments:

Post a Comment