Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is method hiding a form of Polymorphism?

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?

like image 886
Jeeshu Mittal Avatar asked Sep 29 '22 16:09

Jeeshu Mittal


1 Answers

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.

like image 106
Evgeniy Dorofeev Avatar answered Oct 06 '22 18:10

Evgeniy Dorofeev