File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instantiating hidden classes (static)
 
Similar Threads
static methods Help plz..
Invocation of Hidden Class Methods
Tricky question
abstract and static
static methods/casting question