aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q about polymorphism 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q about polymorphism" Watch "Q about polymorphism" New topic
Author

Q about polymorphism

Kathy Cai
Greenhorn

Joined: Jan 13, 2004
Posts: 13
Both show1 and show2 in Q2 class are overriding methods in the attached code. However if I run the code, I got the following results:
Show1 in Test class
Show2 in Q2 class
That is, show1 in Test class was not overridden. I don't know why modifier "static" caused this. I appreciate your explanation.
----
class Test {
static void show1() {
System.out.println("Show1 in Test class"); }
void show2() {
System.out.println("Show2 in Test class"); }
}
public class Q2 extends Test {
static void show1() {
System.out.println("Show1 in Q2 class"); }
void show2() {
System.out.println("Show2 in Q2 class"); }
public static void main(String[] args) {

Test t = new Q2();
t.show1();
t.show2();
}
}
Bojan Knezovic
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Kathy,
static methods are never overrided. It's simple as that.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Although this looks like method overriding, it is not. As was said previously, static methods can not be overridden - rather, they are hidden. Hidden methods and overridden methods behave very differently.
Be sure to check out the JLS, §8.4.6 Inheritance, Overriding, and Hiding for details.


SCJP Tipline, etc.
Kathy Cai
Greenhorn

Joined: Jan 13, 2004
Posts: 13
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q about polymorphism
 
Similar Threads
Static method
Abstract Class Within an Interface
Overriding Issue
how to work with interface in java?
Confusion