1

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 ?

  • 4
    Why do you claim that you can override a static method? You can hide it, but that's quite a different thing. – CodesInChaos Oct 4 '14 at 7:50
  • To add to what @CodesInChaos said: Java has an Override annotation that you can add to overriding methods, which makes the compiler give you an error if the method doesn't actually override anything. You will most likely find that you get an error if you add this annotation to a static method. – Sebastian Redl Aug 25 '16 at 8:51
12

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:

enter image description here

  • 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.

  • If you'd gave the static methods the same name it perhaps would've made sense. Try x2.printStatic() and the outcome will be hi static from A. – abto Oct 4 '14 at 8:43
  • @abto I corrected the error. Thanks for pointing it out. Results are the same. – Tulains Córdova Oct 4 '14 at 8:49
  • 4
    Static methods are called static because (a) they aren't methods but namespaced functions, and because (b) you explicitly request to forego dynamic dispatch, which is the central feature of OOP. Instead of dispatching on the run-time (aka dynamic) type of an object, you dispatch static methods on the compile-time (aka static) type of the variable or expression, which leads to the observed behaviour. It's important to understand how static methods are not “class methods”. – amon Oct 4 '14 at 15:05
2

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.

  • Correct but I've always found it confusing that method shadowing is considered polymorphism. The two methods in this case are not related to each other in any way other than that they have the same simple name. They have different qualified names that and are no more related to each other than they are to any other static method with a different name. – JimmyJames Aug 25 '16 at 17:00
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.

  • your post refers site how-to-program-in-java.com and the same site is shown in your profile, are you affiliated with that site? If yes, please edit the post to disclose that. See Help Center -> How to not be a spammer – gnat Aug 25 '16 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.