Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
suneel kanth
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sk dev, what do you understand Polymorphism to mean? What do you understand Inheritance to mean?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic