• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Static Member Override???

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you are seeing what you have read, b'coz static method message in base class is showing up for b.message which proves that your overriding method in subclass is not executed.
This feature of static methods is called 'hiding'.
hence static methods cannot be overridden.

[This message has been edited by Harsha Jay (edited November 08, 2001).]
 
vinay jain
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can u tell me more about hiding and how is it different then overriding? Also is there other situations in which the hiding can occur?
 
Harsha Jay
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just do a search on Hiding ...
you will end up with lot of info on hiding.
otherwise let me know.
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hehhee..
read this link
http://www.javaranch.com/ubb/Forum24/HTML/013038.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic