Overriding member variables in Java ( Variable Hiding)
When you make a variable of the same name in a subclass, that's called hiding. The resulting subclass will now actually have both properties. You can access the one from the superclass with super.var
or ((SuperClass)this).var
. The variables don't even have to be of the same type; they are just two variables sharing a name, much like two overloaded methods.
Is there a way to override class variables in Java?
Yes. But as the variable is concerned it is overwrite (Giving new value to variable. Giving new definition to the function is Override). Just don't declare the variable but initialize (change) in the constructor or static block.
The value will get reflected when using in the blocks of parent class
if the variable is static then change the value during initialization itself with static block,
class Son extends Dad {
static {
me = "son";
}
}
or else change in constructor.
You can also change the value later in any blocks. It will get reflected in super class
Explain how variable hiding is working in this Java code
Yes, the B
class inherits the foo
method. But the variable x
in B
hides the x
in A
; it doesn't replace it.
This is an issue of scope. The foo
method in A
sees only the variables that are in scope. The only variable in scope is the instance variable x
in A
.
The foo
method is inherited, but not overridden, in B
. If you were to explicitly override foo
with the same exact code:
class B extends A
{
int x = 6;
@Override
void foo()
{
System.out.println(this.x);
}
}
Then the variable that would be in scope when referred to by this.x
would be B
's x
, and 6
would be printed. While the text of the method is the same, the reference is different because of scope.
Incidentally, if you really wanted to refer to A
's x
in the B
class, you can use super.x
.
Overriding a super class's instance variables
Because if you changed the implementation of a data member it would quite possibly break the superclass (imagine changing a superclass's data member from a float to a String).
Hiding instance variables of a class
In Java, data members are not polymorphic. This means that Parent.var
and Child.var
are two distinct variables that happen to have the same name. You're not in any sense "overriding" var
in the derived class; as you have discovered yourself, both variables can be accessed independently of one another.
The best way forward really depends on what you're trying to achieve:
- If
Parent.var
should not be visible toChild
, make itprivate
. - If
Parent.var
andChild.var
are two logically distinct variables, give them different names to avoid confusion. - If
Parent.var
andChild.var
are logically the same variable, then use one data member for them.
Java method overriding and variable shadowing
Method calls are virtual in Java, which means that the method from an object's actual type is called, regardless of what type the reference was which you used to access the object. Direct field access on the other hand is not virtual, so which r
you access will depend on the type of the reference through which you reached it.
Java Inheritance - instance variables overriding
Because inheritance is intended to modify behaviour. Behaviour is exposed through methods, and that's why they can be overridden.
Fields are not behaviour but state. You don't need to modify that, nor the private methods employed by the superclass. They are intended to allow the superclass to do it's job.
Related Topics
Are Getters and Setters Poor Design? Contradictory Advice Seen
How to Determine Day of Week by Passing Specific Date
How to Handle Events from Keyboard and Mouse in Full Screen Exclusive Mode in Java
What Does ≪T≫ (Angle Brackets) Mean in Java
What Are the Effects of Exceptions on Performance in Java
Value Change Listener to Jtextfield
Convert Arraylist≪String≫ to String[] Array
How to Programmatically Close a Jframe
How Can a Java Program Get Its Own Process Id
What Are All the Escape Characters
Bringing Jfilechooser on Top of All Windows
Difference Between ≪Context:Annotation-Config≫ and ≪Context:Component-Scan≫
Normalization in Dom Parsing With Java - How Does It Work
Sort Objects in Arraylist by Date