Question:
In several articles I read this:
Method overloading supports the property of polymorphism, because this is how C # implements the main principle of polymorphism: one interface, many methods.
After talking with colleagues about this, most said that method overloading (overload) has nothing to do with polymorphism. Is it so?
Answer:
Yes and no. It depends on what they mean by polymorphism.
In general , polymorphism is the ability to treat entities of different types in the same way . Or, a little more scientifically, the existence of a common interface (apart from C #) for entities of different types. This is a very general concept that can be pulled on a lot. What happens in this case and what abstractions of the language will be involved in this is not so important.
Method overloading refers to this polymorphism in the most direct way: it is ad-hoc polymorphism for a function: different types can be passed to it (within the boundaries of the implemented overloads) and different code will be executed. (And methods of class objects can be viewed as functions with an additional implicit argument: an object whose method is called [ this
in sharpe].)
A natural question may arise: are not then different overloads different functions, does this not break the correspondence to the definition of polymorphism? Answer: polymorphism exists at the language level, and if in a language overloads with the same names but different types are considered the same function, then the definition is met and everything is ok.
By the levels below, of course, different code is placed in different places and the implementation somehow has to point to different places, technically treating the passed values differently. But you do not work directly with these levels, if there is polymorphism (which is unlikely), then it is not quite (or not at all?) The one that you use.
In OOP, polymorphism is usually understood as a much narrower concept: the ability to call the same method on any object of a given class / interface or any of its subclasses / implementation.
Overloading really has nothing to do with this polymorphism.