Many say that we cannot override a static methods.
But we can override a static method.
The question is, when we override a static method why it does not result in polymorphism ?
2
Interesting question, I tested it this way:
Superclass
public class ClassA {
public static void printStatic(){
System.out.println("hi static from A");
}
public void printDynamic(){
System.out.println("hi dynamic from A");
}
}
Subclass
public class ClassB extends ClassA {
public static void printStatic(){
System.out.println("hi static from B");
}
public void printDynamic(){
System.out.println("hi dynamic from B");
}
}
Test code
public static void main (String args[]){
ClassA x1 = new ClassB();
x1.printStatic();
x1.printDynamic();
ClassB x2 = new ClassB();
x2.printStatic();
x2.printDynamic();
}
Output
hi static from A
hi dynamic from B
hi static from B
hi dynamic from B
Analysis of test output
Calling a static method allways executes the method in the declaring type, not the overriden method in the instantiated type (no polymorphism), whereas calling a dynamic method always executes the overridden method in the instantiated type (polymorphism).
My explanation
I think that’s because static methods belong to classes, not to objects. That’s why eclipse suggest you to call the method in a static way using the name of the class and not the name of the object:
- Static methods always refer to the declaration type.
- It’s the same reason static fields/members/attributes are the same to every object, because they only exist once, in the class itself.
- By calling a static method you are specifically telling Java you want the method in the declaring type executed.
I hope my answer doesn’t sound like Captain Obvious wrote it.
3
It is polymorphism, but not the same kind of polymorphism as when overriding instance methods.
It is really a question about terminology. A static method with the same name in a subclass is a form of ad-hoc polymorphism, somewhat similar to method overloading. The word overloading (rather than overriding) is usually used with ad-hoc polymorphism (like in “operator overloading” which is also a kind of ad-hoc polymorphism). But overriding instance methods is subtype polymorphism which is a different thing.
When talking about polymorphism in OO, usually subtype polymorphism is meant. With ad-hoc polymorphism, the method to be called is statically resolved at compile time. With subtype polymorphism the method implementation is selected at runtime. Subtype polymorphism is fundamental to object orientation while ad-hoc polymorphism is more of a convenience.
1
Polymorphism has two types:
static polymorphism : achieved using method overloading in java
dynamic polymorphism: achieved using method overriding
Your question deals with dynamic polymorphism.
Dynamic polymorphism is related to inheritance.
Let’s consider the example where child class ClassB override a method called methodA present in the parent class ClassA.
when writing this code :
ClassA c = new ClassB();
c.methodA();
Dynamic polymorphism in java dictates that a dynamic binding occurs here and the method methodA defined in class B will be called.
Static methods belong to the class and not to individual objects. When you define a static method in the child class, this method has no relationship with the parent method.
It is considered as a new method.
You can take a look at these tutorials for more informations about method overriding and polymorphism.
1