One should have a strong understanding of this keyword in inheritance in Java to be familiar with the concept. Instance variable hiding refers to a state when instance variables of the same name are present in superclass and subclass. Now if we try to access using subclass object then instance variable of subclass hides instance variable of superclass irrespective of its return types.
In Java, if there is a local variable in a method with the same name as the instance variable, then the local variable hides the instance variable. If we want to reflect the change made over to the instance variable, this can be achieved with the help of this reference.
Example:
Java
class Test {
private int value = 10 ;
void method() {
int value = 40 ;
System.out.println( "Value of Instance variable : "
+ this .value);
System.out.println( "Value of Local variable : "
+ value);
}
}
class GFG {
public static void main(String args[]) {
Test obj1 = new Test();
obj1.method();
}
}
|
Output
Value of Instance variable : 10
Value of Local variable : 40
Time Complexity: O(1)
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
31 Jan, 2023
Like Article
Save Article