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 ]