• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sk dev, what do you understand Polymorphism to mean? What do you understand Inheritance to mean?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic