wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Instantiating hidden classes (static) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instantiating hidden classes (static)" Watch "Instantiating hidden classes (static)" New topic
Author

Instantiating hidden classes (static)

mario de simone
Greenhorn

Joined: Apr 17, 2001
Posts: 2
Why does the 'greeting' method from 'XSuper' get executed and the 'name' method from 'XSub'. I thought they should be from 'XSub', and the result should be "Hello, Dick". however, it is actually "Goodnight, Dick".

class XSuper {
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }
}
class YSub extends XSuper {
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}
public class Test1 {
public static void main(String[] args) {
XSuper s = new YSub();
System.out.println(s.greeting() + ", " + s.name());
}
}
Fred Hosch
Author
Greenhorn

Joined: Apr 16, 2001
Posts: 23
Static methods don't get overriden, they get hidden. the method
greeting() in YSub hides, but does not override, the method
greeting() in XSuper.
there is no overriding, so resolution of the invocation
s.greeting() depends on the compile-time type class of s,
which is XSuper. It does not matter that s happens to refer
to a YSub at run-time.
------------------
Fred Hosch
Author of:
An Introduction to Software Construction with Java


Fred Hosch<br />Author of:<br /><a href="http://www.amazon.com/exec/obidos/ASIN/0471354899/ref=ase_electricporkchop/107-7882751-0234939" target="_blank" rel="nofollow">An Introduction to Software Construction with Java</a>
mario de simone
Greenhorn

Joined: Apr 17, 2001
Posts: 2
Thanks for your help Fred. I really appreciate it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Instantiating hidden classes (static)
 
Similar Threads
Invocation of Hidden Class Methods
static methods/casting question
abstract and static
static methods Help plz..
Tricky question