In Java programming, understanding variable scope and inheritance is very important, especially for exams and practicals. One common situation that confuses students is when a superclass and subclass both declare variables with the same name.
This concept is called variable hiding. In this article, you will clearly understand how to access hidden variables in Java using a simple and practical example.
Understanding the Problem
Let’s break down the problem in simple terms:
- Class A declares a static variable x
- Class B extends A and declares an instance variable x
- We need to display both values using a method in class B
At first glance, this may look confusing because both variables have the same name. But Java provides a clear way to access them.
Java Program to Access Hidden Variables
class A {
static int x = 10;
}
class B extends A {
int x = 20;
// Method to display both variables
void display() {
System.out.println("Static variable x from class A: " + A.x);
System.out.println("Instance variable x from class B: " + this.x);
}
}
public class VariableAccessExample {
public static void main(String[] args) {
B b = new B();
b.display();
}
}
Explanation of the Code
Let’s understand how this program works step by step:
- Class A contains a static variable
x = 10 - Class B extends class A and defines its own variable
x = 20 - This creates a situation where the subclass variable hides the superclass variable
- Inside the
display()method:A.xis used to access the static variable from class Athis.xis used to access the instance variable of class B
So even though both variables have the same name, we can access them using proper references.
Program Output
Static variable x from class A: 10 Instance variable x from class B: 20
Real-World Understanding
Think of this like a family scenario:
- Parent (Class A) has a property called x
- Child (Class B) also defines its own property x
- Both exist, but the child’s version hides the parent’s one
To access:
- Parent’s value → use class name (
A.x) - Child’s value → use current object (
this.x)
Common Mistakes Students Make
- Trying to access static variable using object instead of class name
- Forgetting to use
thiskeyword for instance variables - Assuming one variable replaces the other (they both exist)
- Confusing method overriding with variable hiding
Best Practices
- Always use ClassName.variable for static variables
- Use this.variable for instance variables
- Avoid same variable names unless required for learning
- Write clear display methods for debugging and understanding
Pro Tip
Variable hiding is different from method overriding. In overriding, methods are resolved at runtime, but variables are resolved at compile time. This is a very important exam point.
Frequently Asked Questions
What is variable hiding in Java?
Variable hiding occurs when a subclass declares a variable with the same name as in its superclass.
Can we access hidden variables?
Yes, using the class name for static variables and this keyword for instance variables.
Is variable hiding same as overriding?
No, overriding applies to methods, while hiding applies to variables.
🔗 Related Articles
- Java Method Overriding Explained with Examples
- Final Class in Java with Simple Explanation
- Multiple Inheritance in Java Using Interfaces
- Abstract Class in Java with Shape Example
Conclusion
In this tutorial, you learned how to access hidden variables in Java using inheritance. By understanding the difference between static and instance variables and using proper references like A.x and this.x, you can easily handle such situations in exams and real programs.
Make sure to practice this concept with different examples. Don’t forget to bookmark this guide and explore more Java practical programs from the related articles above.

Comments
Post a Comment