aspose file tools*
The moose likes Java in General and the fly likes static methods do not participate in polymorphism.?Attn:Dirk Schreckmann Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "static methods do not participate in polymorphism.?Attn:Dirk Schreckmann " Watch "static methods do not participate in polymorphism.?Attn:Dirk Schreckmann " New topic
Author

static methods do not participate in polymorphism.?Attn:Dirk Schreckmann

suneel kanth
Greenhorn

Joined: Feb 06, 2003
Posts: 22
Hi
I have seen your ans. for one of our friends question...you gave like
"static methods do not participate in polymorphism. They are inherited."
I have not understood completely that ("static methods do not participate in polymorphism") means
what?
I have written a small program...just let you clear specifically with my program...If Iam completely wrong pls. explain with small Example...
public class Poly
{
public static void method1(int x){}
public static void method1(String y){}
public static void main(String args[])
{
Poly.method1(12);
Poly.method1("HI");
}
}
Thank you very much....


Any sufficiently advanced bug is indistinguishable from a feature.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well, since you are really asking for Dirk to reply - I guess that I won't . . .
.
.
.
.
Aw, what the heck :roll:
You have shown an example of overloading. That has nothing to do with polymorphism (well that at least is another whole conversation).
Polymorphism has to do with late binding and relates to overriding methods from a super class . At runtime the JVM decides which version of the method to use based on the type of the object (as opposed to the type of the variable).
In static methods the decision on which method to use is made at compile time. "Static" basically means "not dynamic", as in "can't change". Static methods can be inherited by a subclass, but if the sub-class has a method with the same signature, then the superclass version is hidden (not overridden).

If both classes have a method with the same signature and they are NOT static, then at runtime the JVM will find the method of the Subclass and execute that. That is polymorphism.
If both classes have a method with the same signature and they ARE static, then at compile time the decision is made to use the method of the Superclass, because that is the "type" of the variable and there is no polymorphism going on.
Now if you have:

Where they both have a static method with the same signature, then the method of the sub will be used, because it is "hiding" (or preventing the inheritance of) the supers method. You COULD invoke the static method of the parent by replacing the line
sub.method();
with
Superclass.method();
An example of static methods is shown in the JLS.
8.4.8.5 Example: Invocation of Hidden Class Methods
A hidden class (static) method can be invoked by using a reference whose type is the class that actually contains the declaration of the method. In this respect, hiding of static methods is different from overriding of instance methods. The example:
class Super {
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}
class Test {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name());
}
}
produces the output:
Goodnight, Dick
because the invocation of greeting uses the type of s, namely Super, to figure out, at compile time, which class method to invoke, whereas the invocation of name uses the class of s, namely Sub, to figure out, at run time, which instance method to invoke.

[ February 18, 2003: Message edited by: Cindy Glass ]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Another very nice explanation from Cindy.
Also, to help get a better idea as to what Polymorphism means, take a look at the "How My Dog Learned Polymorphism" story over in The JavaRanch Campfire Stories.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
sk dev, what do you understand Polymorphism to mean? What do you understand Inheritance to mean?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static methods do not participate in polymorphism.?Attn:Dirk Schreckmann