5

Polymorphism is the ability to take many forms. Method overriding is runtime polymorphism.

My questions are:

  1. Is there anything like static polymorphism in Java?

  2. Can method hiding be considered a form of polymorphism?

In this question's answer, it is said that static methods are not polymorphic. What is the reason for that?

Community
  • 1
  • 1
Jeeshu Mittal
  • 445
  • 3
  • 14

5 Answers5

1

Polymorphism

Static Binding/Early binding/Compile time binding - Method overloading.(in same class)
Dynamic binding/Runtime binding/Method overriding.(in different classes.)

Polymorphism in java

It just has two types, Method overloading and Method overriding, as soon as the method overriding turn into Method Hiding, it loses it's polymorphism features.

refer to below question from stackoverflow.

1.) Question1

2.) Question2

Community
  • 1
  • 1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • 1
    Please correct me if I am wrong. Method overloading is not a form of Polymorphism because we are changing the method signature. So same method is not taking different form. Two different methods with same name and having different method signatures are there. Thus, it is not a form of Polymorphism... As per Head First Java – Jeeshu Mittal Feb 25 '15 at 04:49
  • @JeeshuMittal Yes—especially because the method to be invoked is determined statically. – wchargin Feb 25 '15 at 04:55
  • @JeeshuMittal answer added, links added for your exact questions. – Ankur Singhal Feb 25 '15 at 05:04
1

Polymorphism at runtime takes the form of "dynamic dispatch". That is, the actual method that gets called is determined based on the actual instance you are invoking the method on. Obviously, this applies only when you have an instance of a class, so strictly speaking, polymorphism does not apply to hiding of static methods. For further explanation of the difference check here.

amahfouz
  • 2,328
  • 2
  • 16
  • 21
1

If we run this test

class A {
    static void x() {
        System.out.println("A");
    }
}

class B extends A {
    static void x() {
        System.out.println("B");
    }
}

class Test {
    public static void main(String[] args) throws Exception {
        A a = new B();
        a.x();
    }
}

it will print A. If method x() were polymorphic, it would print B.

Kshitij
  • 361
  • 1
  • 9
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Yes, it is indeed helpful. May i know what is the reason for such behaviour? – Jeeshu Mittal Feb 25 '15 at 05:02
  • 1
    I would put it this way: polyphormism is one of OOP features, OOP is about objects, static methods do not work with objects but directly on classes, that is static methods are not OOP – Evgeniy Dorofeev Feb 25 '15 at 05:05
  • 1
    @JeeshuMittal If the method is virtual (which all instance methods are in Java), it doesn't bind at compile time, it looks up what method to run at runtime based on the actual object type, not its declared type. – Lesleh Feb 25 '15 at 05:06
1
  1. Polymorphism could be static and dynamic both. Overloading is static polymorphism while, overriding is dynamic polymorphism. Overloading in simple words means two methods having same method name but takes different input parameters. This called static because, which method to be invoked will be decided at the time of compilation Overriding means a derived class is implementing a method of its super class.
bittu
  • 337
  • 2
  • 13
0

I believe Method Hiding would technically be considered polymorphic. By definition, a hidden method has the same signature, or form, as one found in its base class. This is just one of its "many forms". Think of it as Overloading... that happens to "override" the exact same signature. This would be static polymorphism.

Brandon Petty
  • 605
  • 1
  • 6
  • 15