What is Variable and Method Hiding in Java - Example Tutorial

If Java, if you are not careful you can possibly hide both methods and variables of the superclass. Now you must be wondering what does it mean by hiding a variable or method in Java? 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 variables and methods hiding in Java so that you can understand and avoid them in the future. Earlier, I also wrote about difference between overloading, overriding, shadowing, and hiding, it's a comprehensive post and I suggest you to read it as well if you haven't read already.  It will help you to understand these concepts in more detail. 


What are the variable and methods hiding in Java?

Now that you have some ideas of what is variables and methods hiding in Java, it's time to look at them in more detail. Let's see code examples to understand method hiding and variable hiding in Java programs.

1. Method Hiding in Java - Example

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 that 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 a superclass because a static method cannot be overridden in Java.

Here is an example of a 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 the static method is resolved at compile-time, both are p.sleep() and c.sleep() is 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 the 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 courses and covers Java 17.

Java - Variable and Method Hiding Example



2. Variable Hiding in Java - Example

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.


Other Java Overloading and Overriding Articles you may like
  1. Can you overload a static method in Java? (answer)
  2. My favorite free courses to learn Java? (free courses)
  3. Can you override a static method in Java? (answer)
  4. 10 Java online courses for beginners (best courses)
  5. Why Java doesn't support operator overloading? (answer)
  6. Java or Python? which is a better language for beginners (article)
  7. Can you overload or override the main method in Java? (answer)
  8. Rules of method overriding in Java? (answer)
  9. Difference between overriding, overloading, shadowing, and obscuring in Java? (answer)
  10. What is Polymorphism? Method Overloading or Overriding? (answer)
  11. 19 Method overloading and overriding interview questions? (list)
  12. Constructor and Method Overloading best practices in Java (best practices)
  13. Difference between method overloading and overriding? (answer)
  14. What is the real use of overloading and overriding? (answer)
  15. What is Constructor overloading in Java? (answer)

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

P. S. - If you are keen to learn Java Programming in-depth but looking for free online courses then you can also check out Java Tutorial for Complete Beginners (FREE) course on Udemy. It's completely free and you just need an Udemy account to join this course.

And now one questions for you, can you overriding final method in Java? Also, what about static methods? can you override static methods in Java?Both of them are popular interview question and see if you can answer them in comments. 

1 comment:

  1. No, you cannot override a final method in Java but you can overload a final method. Similarly, static method cannot be overridden because its resolved at compile time not runtime.

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.