In OOP, is operator overloading in fact polymorphism or parameter overloading?
is operator overloading in fact polymorphism or parameter overloading?
Is it true that polymorphism usually refer to different classes responding to the same "message" (the method name) and do differen开发者_高级运维t things, so
bird.give_sound()
and
car.give_sound()
can do different things. And parameter overloading is more about talking about the same class, doing different things when the parameters sent along with the message (the method name) are different. So
bird.give_sound()
and
bird.give_sound(:frighten_sound)
can be different.
So operator overloading is strictly parameter overloading? like this following:
"foo" + "bar"
"foo" + 3
at least in Ruby, it is sending the +
message to the string containing foo
, the first line is sending with a parameter string, the second one is sending a parameter 3, and the +
do slightly different things, but it is the same receiver class String
In the following example, it is polymorphism:
"foo" + 3
1 + 3
because the +
message invoke different methods of different classes, but using the same message name +
. So in these 2 cases, they are polymorphism, not operator overloading?
Is the above accurate and correct? Is there something that might be added to it or be corrected above?
Thank you for the clarification of context in your comment. Yes, I would say you are correct.
To summarize as short as possible...
Given identical method name (or "message"):
- different behaviour based on parameter type is overloading,
- different behaviour based on object type is polymorphism.
I'm going to take a stab in the dark and say that it's kind of (but not really) both.
Each object has to deal with the given object (the object on the right side of the operator) in a specific way. With strings, it seems that the toString
method (or other language equivalent) would be used. So you would always be appending a string (passed to the method as an Object
). (Polymorphism)
However, your object may need to perform different logic based upon the object given. For example, say you have a Student
object. You may have one version of the method that takes a Class
object, and adds it to the Student's class schedule. You then might have an overload that takes, say, a Book
and adds it to the Student's collection of required reading material. (Parameter Overloading)
Polymorphism is when one data type dynamically behaves as another datatype.(Dynamic typecasting)
Shape
Qudrilateral
Rect
Rhombus
Elliptoid
Oval
Circle
Polymorphism is automatically selecting the proper area() method for a given object context
Operator overloading is when you select the correct area method for a method context(i.e. number of arguments passed or type of arguments passed) So if Rect had two area methods, one that accepted one argument(square) and one that accepted two arguments(any other rectangle)
So depending on the usage context, defining the operators for a given object can result in either Polymorphism or Operator Overloading.
Good Question.
The problem you found, its that we have 2 different concepts which similar syntax, that clash when applied programming: Overloading and parameter inheritance.
When I found operator overloading, I usually think in terms of overloading (methods) functions, to make more clear.
When I read these:
// (string) x = (string) "foo" + (int) 3 x = "foo" + 3
I think of these:
// (string) x = (string) "foo".concat((int) 3) x = "foo".concat(3)
There is an additional problem, that each programming language handles operators with classes different.
I would suggest, to avoid operator overloading with object parameters, and explicity use functions.
精彩评论