Is the method a hiding form of polymorphism?

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

My questions:

  • Is there something like static polymorphism in Java?

  • Is it possible to hide the method as a form of polymorphism?

This answer to the question says that static methods are not polymorphic. What is the reason for this?

+5
source share
5 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(); ax(); } } 

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

+1
source

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 simply has two types: Method overloading and Method overriding , as soon as Method overriding becomes Method Hiding , it loses its polymorphism functions.

see below question from stackoverflow.

1.) Question1

2.) Question2

+1
source

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

+1
source
  • Polymorphism can be static and dynamic. Overloading is static polymorphism, and overriding is dynamic polymorphism. Overloading in simple words means two methods that have the same method name, but accept different input parameters. This is called static, because which method will be called will be decided at compile time. Overriding means that the derived class implements the method of its superclass.
+1
source

I believe the Hiding method is technically considered polymorphic. By definition, a hidden method has the same signature or form as in the base class. This is just one of its "many forms." Think of it as overloading ... what happens with the "redefinition" of the same signature. This will be static polymorphism.

0
source

Source: https://habr.com/ru/post/1214131/


All Articles