Is the method an overload of a form of polymorphism or something else?

I have long-standing doubts. Can someone please tell me if method overloading is a form of polymorphism or is it something completely different?

+4
source share
5 answers

The overload method is just syntactic sugar, allowing you to have methods with the same name, but with different arguments. This has nothing to do with polymorphism. Method overloading is usually used to define two methods that take different arguments, for example:

public void println(boolean x) //... public void println(char x) //... 

or skip certain parameters and use some default values:

 public String substring(int beginIndex) //... public String substring(int beginIndex, int endIndex) //... 

The redefinition method, on the other hand, is the basis of inheritance and is more closely related to polymorphism.

+7
source

Polymorphism literally means something that has multiple behaviors.

In java, we can have static and temporary polymorphism.

Overloading is a static polymorphism, as it allows different behavior by passing different arguments. But this is only allowed at runtime, therefore it is static.

Overriding is dynamic polymorphism, since the actual function call depends on the type of object that calls it, which is available only at run time, therefore dynamic.

+3
source

No, it is not.

When overloaded, you simply provide different implementations of the same method name with different signatures.

Since polymorphism (by subtyping) requires the same signature (which is produced either by the name of the method or by parameters), two things can never intersect.

+2
source

No, it is not, it is an overload method.

java does polymorphism through interfaces. It does not have multiple inheritance.

You can, however, simulate multiple inheritance using multiple interfaces and a compound / delegate pattern.

+1
source

No, this is not related to object-oriented programming. Overloading simply means that you can use the same name for different method signatures.

+1
source

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


All Articles