Technology

Difference between variable hiding and method hiding in Java

sahilsaini122
sahilsaini122
6 min read

In Java, when a subclass defines a variable or a method with the same name as the variable or method defined in the superclass, it is called "hiding." Variable hiding and method hiding are two different concepts in Java. Variable hiding occurs when a subclass defines a variable with the same name as the variable in the superclass, whereas method hiding occurs when a subclass defines a method with the same name as the method in the superclass but with a different signature.

 

Variable hiding

Variable hiding is a phenomenon in Java where a variable declared in a subclass or a method has the same name as a variable declared in its superclass or the class itself. In such a scenario, the variable declared in the subclass or method is said to "hide" the variable declared in the superclass or class.

When a variable is hidden, it means that any reference to that variable within the scope of the subclass or method will refer to the variable declared in the subclass or method, even if there is a variable with the same name declared in the superclass or class. This can lead to unexpected behavior in the program and can make it harder to debug.

To avoid variable hiding in Java Online Compiler, it is recommended to use unique and descriptive names for variables in the subclass or method. If a variable with the same name as a variable in the superclass or class is required, it is recommended to use the "super" keyword to reference the variable in the superclass or class.

Variable hiding can cause confusion and unexpected behavior in Java programs, so it is important to use unique and descriptive names for variables and to use the "super" keyword when referencing variables in the superclass or class.

 

Method hiding

Method hiding is a phenomenon in Java where a subclass declares a static method with the same signature (method name and parameters) as a static method declared in its superclass. When this happens, the method in the subclass is said to "hide" the method in the superclass.

Method hiding is different from method overriding, which occurs when a subclass overrides a non-static method in its superclass with a method of the same name and signature, but potentially different implementation. method hiding in Java only occurs with static methods, and the method in the subclass does not override the method in the superclass. Instead, the method in the subclass is considered a separate method that happens to have the same name and signature as the method in the superclass.

When a static method is called on an object, the method that is called depends on the type of the reference variable, not the type of the object itself. This means that if a static method is declared in a superclass and a subclass, and a method call is made on an object of the subclass, the method in the subclass will be called if it has the same signature as the method in the superclass. This can lead to unexpected behavior and is generally considered bad practice in Java.

To avoid method hiding in Java, it is recommended to use unique and descriptive names for static methods in subclasses and to avoid declaring static methods in subclasses with the same signature as static methods in superclasses.

Method hiding can cause confusion and unexpected behavior in Java programs, so it is important to use unique and descriptive names for static methods and to avoid declaring static methods in subclasses with the same signature as static methods in superclasses.



Variable hiding and method hiding are both concepts in Java Online Compiler, but they are different phenomena.

Variable hiding occurs when a subclass or method declares a variable with the same name as a variable declared in its superclass or the class itself. When this happens, the variable declared in the subclass or method "hides" the variable declared in the superclass or class. Any reference to that variable within the scope of the subclass or method will refer to the variable declared in the subclass or method, even if there is a variable with the same name declared in the superclass or class.

Method hiding, on the other hand, occurs when a subclass declares a static method with the same signature (method name and parameters) as a static method declared in its superclass. When this happens, the method in the subclass is said to "hide" the method in the superclass. When a static method is called on an object, the method that is called depends on the type of the reference variable, not the type of the object itself. This means that if a static method is declared in a superclass and a subclass, and a method call is made on an object of the subclass, the method in the subclass will be called if it has the same signature as the method in the superclass.

In summary, variable hiding occurs with variables, while method hiding occurs with static methods. Variable hiding can cause unexpected behavior in a program, while method hiding can lead to confusion and may be considered bad practice in Java. To avoid these issues, it is important to use unique and descriptive names for variables and methods and to avoid declaring variables or static methods in subclasses with the same name or signature as variables or static methods in superclasses.

In conclusion, variable hiding and method hiding are two important concepts in Java programming. Variable hiding occurs when a subclass defines a variable with the same name as the variable in the superclass, and it can lead to confusion and unexpected results. Method hiding occurs when a subclass defines a method with the same name as the method in the superclass but with a different signature, and it can be useful in certain situations. It is essential to understand the differences between these two concepts to avoid unexpected behavior in Java programs.

Discussion (0 comments)

0 comments

No comments yet. Be the first!