Overload method is polymorphism?

Asked

Viewed 1,006 times

4

I’m having trouble understanding the concept of polymorphism in Java, I read two articles of the same portal now and I was more confused, because for my interpretation they contradict each other:

Stretch 1:

" Method overload (Overload) is a concept of polymorphism that consists basically in creating variations of the same method, i.e., creating two or more methods with totally equal names in a class. Overload allows us to use the same name in more than one method as long as its argument lists are different so that they are separated."

https://www.devmedia.com.br/sobrecarga-e-sobreposicao-de-metodos-em-orientacao-a-objetos/33066

Stretch 2:

" Polymorphism is the principle whereby two or more classes derived from the same superclass may invoke methods that have the same identification, signature, but distinct, specialized behaviors for each derived class, using for this purpose a reference to an object of the superclass type. Overload is not a type of polymorphism, because with Overload the signature of the method must have different arguments, requirement that hurts the concept of polymorphism mentioned above " https://www.devmedia.com.br/encapsulamento-polimorfismo-heranca-em-java/12991

So method overload is polymorphism too?

1 answer

6


In general, publications, some even well-known, can be wrong. In fact, everyone can. There’s a lot of wrong stuff out there, often just for some carelessness.

Note that in this case they are different articles from different authors, so it is normal that each one has a view, here you will see another(s). One is not always wrong just because it is different from the other. Not in this case, but it is also good to always look at the date of publication. And the author’s credentials, because paper and HTML accept everything.

The overload of methods (Overload) is a concept of polymorphism that consists basically in creating variations of the same method, ie the creation of two or more methods with totally equal names in a class. Overload allows us to use the same name in more than one method as long as its argument lists are different so that they are separated

Could be a polymorphism ad-hoc, but people don’t usually use this term in everyday life.

The text is confusing and incomplete, but it is technically correct. The article as a whole is full of strange and even clearly incorrect things in the details.

Polymorphism is the principle whereby two or more classes derived from the same superclass may invoke methods that have the same identification, signature, but distinct, specialized behaviors for each derived class, using for this purpose a reference to an object of the superclass type. Overload is not a type of polymorphism, because with Overload the signature of the method must have different arguments, requirement that hurts the concept of polymorphism mentioned above

Why do you need at least two classes? Isn’t one enough? It’s already started badly. usando para tanto uma referência a um objeto do tipo da superclasse seems confused or just incorrect even. I like this school more to consider the Overload as something different, although theoretically it is, in practice we do not use this term.

He should quote where there is the requirement that the signature should be equal to be polymorphism. I have the impression that he can not, this has no foundation. I say so because he is basing. If you were just to quote, okay, if it’s just to use different terms for each mechanism, that’s good to make it easier to communicate the intention.

The article has other things kind of "loose".

to be polymorphism there has to be overload of method, pus as wrong, so I hit right?

There are controversies. The classical overload is horizontal, is to have methods with the same name, but with different signatures. Some call vertical overload polymorphism where an identical method in the signature behaves differently depending on the level of inheritance it is in. But I prefer to call only polymorphism to avoid confusion. And it can occur dynamically, the most common, or static in general with templates or Generics.

I wouldn’t trust the articles.

  • Thank you so much for the tips!

  • 1

    @Douglasdasilva take a look at [tour] the best way to say thank you.

  • "Why do you need at least two classes? Not just one?". I thought the same thing when I was reading the question!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.