If you are not careful in Java, you may inadvertently hide the methods and variables of the superclass. It is important to understand what it means to hide a variable or method in Java. When a field or variable in a subclass shares the same name as a field in its superclass, it is said to hide the field in the superclass. Similarly, when a subclass has a static method with the same name as a method in the superclass, it can hide the superclass method. This behavior can introduce subtle bugs, especially if you expect a different method to be invoked.
In this article, we will explore examples of both variable and method hiding in Java, providing insights to help you understand and avoid such issues in your code going forward.
Variable and Method Hiding in Java
Variable and method hiding in Java refer to the situation where a variable or method in a subclass shares the same name as a variable or method in its superclass, effectively hiding the superclass member. This behavior can lead to unexpected results and bugs if not handled carefully.
Having gained an understanding of variables and method hiding in Java, let's go deeper into these concepts by examining code examples. This will help us grasp how method and variable hiding occurs in Java programs.
1. Method Hiding:
Method hiding occurs when a static method in a subclass has the same name as a static method in the superclass. Since static methods cannot be overridden in Java, the subclass method hides the superclass method instead of overriding it. This means that the method called depends on the reference type rather than the actual object type.
Let's consider an example to illustrate 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 might expect p.sleep() to call the sleep() method from the Parent class, and c.sleep() to call the sleep() method from the Child class, as it would happen with method overriding. However, since sleep() is a static method, method hiding occurs instead of method overriding. The static method is resolved at compile-time based on the reference type. Both p.sleep() and c.sleep() are resolved to the sleep() method of the Parent class because the types of p and c variables are Parent. If you remove the static keyword from both methods, the behavior will align with the method overriding.
It's important to note that in Java, you cannot override a static method as an instance method, or vice versa. If you remove the static keyword from either the subclass or superclass method, you will encounter a compile-time error stating "This instance method cannot override the static method from Parent" or "This static method cannot hide the instance method from Parent."
A static method with the same name and signature in a subclass can hide a static method of the superclass. It's recommended to be aware of this behavior and use it judiciously in your Java programs.
2. Variable Hiding
Variable hiding occurs when a subclass declares a variable with the same name as a variable in the superclass, effectively hiding the superclass variable. This means that the subclass variable shadows the superclass variable.
Here's an example to illustrate variable hiding in Java:
class Parent {
public String name = "Parent";
}
class Child extends Parent {
public String name = "Child";
}
public class Code {
public static void main(String[] args) {
Parent p = new Parent();
Child c = new Child();
System.out.println(p.name);
System.out.println(c.name);
}
}
Output:
Parent
Child
In this example, we have a name variable in both the Parent and Child classes. When we create objects of Parent and Child classes and access the name variable, the variable in the corresponding class is used. Therefore, p.name returns "Parent" and c.name returns "Child".
Variable hiding can lead to confusion if not used carefully. It's essential to understand that the variable used depends on the reference type. In this case, the type of p is Parent and the type of c is Child.
To access the superclass variable when a subclass variable is present, you can use the super keyword. For example, super.name would access the name variable of the Parent class.
Understanding variable hiding helps in writing code that avoids ambiguity and ensures the correct usage of variables in different class hierarchies.
Conclusion
Understanding and being aware of variable and method hiding in Java is crucial for writing reliable and maintainable code. By using the right techniques and taking into account the rules of inheritance, you can prevent unintended hiding and ensure the proper behavior of your Java programs
Comments