Mani,
Your doubt is correct. You have to understand one more point in inheritance of var. In case of var inheritance, you can re-declare the inheritad var as whatever you want. This is not like method inheritance. In method inheritance only, we have to be careful about whether we are
overriding or
hiding accordingly we should re-define the method in subclass.
But in case of var, the subclass
hides the inherited ones. JLS says this is
hiding of inherited variables. JLS relaxes the ruls for
var hiding .
In case of method hiding we have to be careful while hiding a static method. We can't hide a static method to be non-static.
But in case of var hiding we can.
Refer to
JLS here Classes /4th paragraph/last sentence
From JLS
--------
The members of a class include both declared and inherited members (�8.2). Newly declared fields can hide fields declared in a superclass or superinterface. Newly declared methods can hide, implement, or override methods declared in a superclass or superinterface
Try this example. This code will compile and run without any problem.
<pre>
class Base {
public static final int BASE_INT = 100;
public final int BASE_INSTANCE_INT = 100;
public final int BASE_FINAL_INT = 100;
}
interface inter {
public static final int INTER_INT = 100;
}
class test extends Base implements inter{
//make inherited (from base class) static var as instance var ....OK
public final int BASE_INT = 100;
//make inherited (from interface) static var as instance var ....OK
public final int INTER_INT = 200;
//make inherited (from base class) instance var as static var ....OK
public static final int BASE_INSTANCE_INT = 300;
//make inherited final (from base class) instance var as non-final var ....OK
public int BASE_FINAL_INT = 400;
void print() {
System.out.println("Base_int = "+BASE_INT);
System.out.println("Inter_int = "+INTER_INT);
System.out.println("Base_instance_int = "+BASE_INSTANCE_INT);
System.out.println("Base_final_int = "+BASE_FINAL_INT);
}
public static void main(String[] args) {
new test().print();
}
}
</pre>
[This message has been edited by maha anna (edited April 24, 2000).]