Hello Everybody
while going through the study material and some books, I read that static members can't be overridden. If that is true can someone please explain why the following code works and why does it generate the given output
code:
<pre>
class StaticBase {
static
String message() {
System.out.println("Base-Message");
return "Base-message";
}
String print() {
System.out.println("Hello Base!!!");
return "Hello Base!!!";
}
}
public class StaticOverride extends StaticBase {
static String message() { // Static Member Override!!!
System.out.println("Derived-Message");
return "Derived-Message";
}
String print() {
System.out.println("Hello Derived!!!");
return "Hello Derived!!!";
}
public static void main(String args[]) {
StaticBase b = new StaticOverride();
System.out.println("b.message=" + b.message() + ", b.print=" + b.print());
}
}
</pre>
Output:
<pre>
Base-Message
Hello Derived!!!
b.message=Base-message, b.print=Hello Derived!!!
</pre>
Thanks
Vinay