Why are we not able to override an instance variable of a super class in a subclass?
-
What do you mean by override a variable? You can access it if it is protected or public, but not private. – Blorgbeard Jan 09 '09 at 11:35
9 Answers
He perhaps meant to try and override the value used to initialize the variable. For example,
Instead of this (which is illegal)
public abstract class A {
String help = "**no help defined -- somebody should change that***";
// ...
}
// ...
public class B extends A {
// ILLEGAL
@Override
String help = "some fancy help message for B";
// ...
}
One should do
public abstract class A {
public String getHelp() {
return "**no help defined -- somebody should change that***";
}
// ...
}
// ...
public class B extends A {
@Override
public String getHelp() {
return "some fancy help message for B";
// ...
}

- 24,012
- 7
- 60
- 96
-
7Also, if you make the method `getHelp()` in the super class `abstract`, you can force the subclasses to provide an implementation for it instead of just hoping they override it. – yellavon Aug 11 '14 at 21:59
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).

- 616,129
- 168
- 910
- 942
Because you can only override behavior and not structure. Structure is set in stone once an object has been created and memory has been allocated for it. Of course this is usually true in statically typed languages.

- 7,907
- 6
- 35
- 53
Variables aren't accessed polymorphically. What would you want to do with this that you can't do with a protected variable? (Not that I encourage using non-private mutable variables at all, personally.)

- 1,421,763
- 867
- 9,128
- 9,194
class Dad{
public String name = "Dad";
}
class Son extends Dad{
public String name = "Son";
public String getName(){
return this.name;
}
}
From main() method if you call
new Son().getName();
will return "Son" This is how you can override the variable of super class.

- 2,998
- 3
- 23
- 52
you can override a method,that is all right but what do you mean by overriding a variable? if you want to use a variable at any other place rather than super class u can use super. as in super(variable names); why do you want to override a variable? i mean is there any need?
we can not overriding structure of instance variables ,but we ovverride their behavior:-
class A
{
int x = 5;
}
class B extends A
{
int x = 7:
}
class Main
{
public static void main(String dh[])
{
A obj = new B();
System.out.println(obj.x);
}
}
in this case output is 5.

- 1
Do you mean with overriding you want to change the datatype for example?
What do you do with this expression
public class A {
protected int mIndex;
public void counter(){
mIndex++;
}
}
public class B extends A {
protected String mIndex; // Or what you mean with overloading
}
How do you want to change the mIndex++ expression without operator overloading or something like this.

- 12,177
- 6
- 40
- 66
If you have the need to override an instance variable, you are almost certainly inheriting from the worng class.
In some languages you can hide the instance variable by supplying a new one:
class A has variable V1 of type X;
class B inherits from A, but reintroduces V1 of type Y.
The methods of class A can still access the original V1. The methods of class B can access the new V1. And if they want to access the original, they can cast themself to class A (As you see dirty programming provokes more dirty progrtamming).
The best solution is to find another name for the variable.

- 52,876
- 38
- 145
- 202