-
difference between polymorphism and overloading
Hi
The passage quoted below is from a book. I'm still not clear about the distinction between 'polymorphism' and 'overloading'. Could you please let me know how to differentiate between the two? Thanks.
Quote:
Using operators or functions in different ways, depending on what they are operating on, is called polymorphism (one thing with several distinct forms). When an existing operator, such as + or =, is given the capability to operate on a new data type, it is said to be overloaded. Overloading is a kind of polymorphism; it is also an important feature of OOP.
-
Re: difference between polymorphism and overloading
The key sentence is "Overloading is a kind of polymorphism". Specifically, overloading is when the same function name or operator symbol is used, but there are multiple functions with that name available that may take different argument types. So the set of arguments determines which function is called.
There are other types of polymorphism as well. For instance, virtual method dispatch in an inheritance hierarchy, in which the type of object being operated on is what determines which method is called. (Since the object's address is passed to the function "behind the scenes", in a sense, it's not all that different. But it looks different to the programmer.)
An additional type of polymorphism seen in C++ is templates. Rather than explicitly specifying the type of the parameters, templates allow any type to be used, so long as the operations conducted on that type within the function make sense for the type. Again, this isn't all that different from overloading except that it saves the programmer some typing since they don't need to define essentially the same function multiple times for different argument types.
-
Re: difference between polymorphism and overloading
Polymorphism is the ability of the compiler to treat a derived class as the base class. So a Herron is a bird so Herron.Flys() because Bird.Flys().
Overloading is the ability of the compiler to pick out which function to called based on the function signature or the parameters.
It knows to call this function
Code:
int Check(Int val){}
as opposed to
Quote:
int Check(Int val1, Int Val2){}
when you write:
Because of the number and type of parameters passed to the function.
-
Re: difference between polymorphism and overloading
Hi there you may try read this maybe it will help:
Short answer:
They are the same.
Long Answer, (and yet less revealing):
Polymorphism is simply the ability to have many different methods (Or functions, for those who are used to C-Type programs) to have the same name, but act differently depending on the type of parameters that were passed to the function.
So for example, we may have a method called punch, which accepts no parameters at all, and returns an integer:
public int punch()
{
return 3;
}
We could also have a method named punch that accepts a String and returns a boolean.
public boolean punch(String poorGuyGettingPunched)
{
if(poorGuyGettingPunched.equals("joe"))
{
System.out.println("sorry Joe");
return true;
}
else
return false;
}
That is called polymorphism... And strangely enough, it is also called overloading.
Do not confuse this with overriding, which replaces a function or method with a new one, or rather, hides the old method and replaces it with a new one.
Read more: http://wiki.answers.com/Q/Difference...#ixzz1aoiAVAkM
-
Re: difference between polymorphism and overloading
The difference between polymorphism and method overloading is in the time when the actual method to execute is determined. The reason for this is that when a method is overloaded, such as in:
account = new BankAccount();
account = new BankAccount(1000);
The compiler can tell which constructor to use by the method signature, including the number and types of parameters provided. This selection of a method to use at compile time, before the program ever runs is called early binding.
-
Re: difference between polymorphism and overloading
Thank you, Lindley, ahoodin.
Quote:
Originally Posted by
Lindley
The key sentence is "Overloading is a kind of polymorphism". Specifically, overloading is when the same function name or operator symbol is used, but there are multiple functions with that name available that may take different argument types. So the set of arguments determines which function is called.
My understanding of C++ is very basic. I haven't read about derived classes, templates, etc.
You have described what overloading is. But what is polymorphism in itself? I mean even if overloading is subset of polymorphism, what thing makes polymorphism a superset? Thanks for your help.
Regards
H
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by heights
But what is polymorphism in itself? I mean even if overloading is subset of polymorphism, what thing makes polymorphism a superset?
The same idea, except that we are not necessarily dealing with multiple functions overloaded on an ad-hoc basis, but perhaps it is a single generic function (or a function template). Or, perhaps the notion of supertype and subtype is involved. Or, perhaps the notion of duck typing is involved. Now, don't ask me to explain what these terms mean: search the Web and find out.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
jmndoza
Hi there you may try read this maybe it will help:
Short answer:
They are the same.
Long Answer, (and yet less revealing):
Polymorphism is simply the ability to have many different methods (Or functions, for those who are used to C-Type programs) to have the same name, but act differently depending on the type of parameters that were passed to the function.
So for example, we may have a method called punch, which accepts no parameters at all, and returns an integer:
public int punch()
{
return 3;
}
We could also have a method named punch that accepts a String and returns a boolean.
public boolean punch(String poorGuyGettingPunched)
{
if(poorGuyGettingPunched.equals("joe"))
{
System.out.println("sorry Joe");
return true;
}
else
return false;
}
That is called polymorphism... And strangely enough, it is also called overloading.
Do not confuse this with overriding, which replaces a function or method with a new one, or rather, hides the old method and replaces it with a new one.
Read more:
http://wiki.answers.com/Q/Difference...#ixzz1aoiAVAkM
That's not polymorphism. Polymorphism means that the correct function will be called based on the object type. Arguments in the based and derived classes have to be the same and the function has to be declared virtual in the base class.
Overloading is when you have functions with the same name but different types.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by GCDEF
That's not polymorphism. Polymorphism means that the correct function will be called based on the object type. Arguments in the based and derived classes have to be the same and the function has to be declared virtual in the base class.
Overloading is when you have functions with the same name but different types.
Overloading is polymorphism: ad-hoc polymorphism; what you stated is supertype/subtype polymorphism.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
heights
You have described what overloading is. But what is polymorphism in itself? I mean even if overloading is subset of polymorphism, what thing makes polymorphism a superset?
Polymorphism simply means 'many forms'. It's an important feature both of programming languages and of program design.
C++ programmers often use polymorphism in a very strict sense namely to distinguish between two C++ language mechanism. This pair goes by several names such as early vs. late binding, compile time vs. runtime polymorphism, static vs. dynamic polymorphism, overloading vs. overriding, etcetera. Some go even further and consider polymorphism to be synonymous with the C++ virtual method dispatch mechanism.
To get a broader view you need to consider the type systems of programming languages. Types are predefined primitives such as int, char, bool, float, etcetera, as well as programmer defined classes/structs. Types basically can show four kinds of polymorphism namely coersion, overloading, inclusion and parametric. I won't go into much details but here's a short summary:
1. Coersion is the ability of types to automagically 'slide' into each other. For example an int becomes a float in an expression, or the other way around.
2. Overloading is the ability of functions/methods/operators to have the same name but different parameter lists. For example you can have two methods named xyz in a class, one taking an int and the other taking an int and a string or something.
3. Inclusion (or subtype polymorphism) is the ability of objects to have many types. This is the 'object oriented' kind of polymorphism. It's based on the language mechanisms of inheritance and method overriding.
4. Finally there's parametric polymorphism. It's the ability to use types as parameters. It's also often called generics and C++ has its own variation called templates. For example you can have a method and when you use it decide which type is to be used internally to perform certain calculations by passing say int or float or some other type.
To summarize polymorphism in just one word I would say flexibility. A monomorphic type system would be very rigid and tedious to use.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
laserlight
Overloading is polymorphism: ad-hoc polymorphism; what you stated is supertype/subtype polymorphism.
Code:
class Cbase
{
void Func();
void Func(int); //Overloading
virtual void PolyFunc();
};
class CDerived : public CBase
{
void PolyFunc();
};
Func is overloaded. PolyFunc is, or can be, polymorphic. It's not really the same concept. Overloading gives a different signature to a function so you can call the function with diffferent argument types and numbers. In simplistic terms, polymorphism means that the function will be called for the correct class when accessed through a base class pointer.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by GCDEF
Func is overloaded. PolyFunc is, or can be, polymorphic. It's not really the same concept. Overloading gives a different signature to a function so you can call the function with diffferent argument types and numbers. In simplistic terms, polymorphism means that the function will be called for the correct class when accessed through a base class pointer.
It is the same overarching concept (see Lindley's post #2, my post #7 and nuzzle's post #10). We just normally use "polymorphism" in C++ to mean subtype polymorphism at runtime. That said, see Stroustrup's definition of polymorphism in his C++ glossary:
Quote:
Originally Posted by Bjarne Stroustrup
polymorphism - providing a single interface to entities of different types. virtual functions provide dynamic (run-time) polymorphism through an interface provided by a base class. Overloaded functions and templates provide static (compile-time) polymorphism.
-
Re: difference between polymorphism and overloading
It is important to understand that while C++ applies the notion of "polymorphism" a certain way, it is in fact a much more general Computer Science concept not linked to any one language:
http://en.wikipedia.org/wiki/Polymor...ter_science%29
-
Re: difference between polymorphism and overloading
Thanks a lot, everyone. I really appreciate your help. But some of the talk here went over my head! :)
Regards
H
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
heights
But some of the talk here went over my head! :)
Well, you got the gist didn't you.
C++ programmers usually have a quite limited outlook of what constitutes polymorphism. A broader view requires you to consider type systems and then there are four basic forms of polymorphism namely coersion, overloading, inclusion and parametric.
With this information you can safely go on learning C++ enjoying the benefits of a very polymorphic type system. What's been said in this thread will become clear to you eventually. Good luck. :)
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
nuzzle
Well, you got the gist didn't you.
C++ programmers usually have a quite limited outlook of what constitutes polymorphism. A broader view requires you to consider type systems and then there are four basic forms of polymorphism namely coersion, overloading, inclusion and parametric.
With this information you can safely go on learning C++ enjoying the benefits of a very polymorphic type system. What's been said in this thread will become clear to you eventually. Good luck. :)
Thank you for the luck! :)
Regards
H
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
nuzzle
C++ programmers usually have a quite limited outlook of what constitutes polymorphism.
Must be having another senior moment.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
ahoodin
Must be having another senior moment.
Yes sometimes you forget that you never knew.
-
Re: difference between polymorphism and overloading
Nope it would be you. You are the Senior member who forgets how to behave and has the senior moments. And I mean plural, because you have had many of them over the past year. You think that you can just blurt anything out about C++ programmers. It is you who have the limited view of C++ programmers. You with the smart allec comments, and you who needs to learn manners.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
ahoodin
Nope it would be you. You are the Senior member who forgets how to behave and has the senior moments. And I mean plural, because you have had many of them over the past year. You think that you can just blurt anything out about C++ programmers. It is you who have the limited view of C++ programmers. You with the smart allec comments, and you who needs to learn manners.
Thank you for expanding my view on C++ programmers. Always well-mannered and never a senior moment right. Maybe I should ask one for a date. You don't happen to be ...
But that won't change the facts. The predominant usage of the term polymorphism in the C++ community is limited compared with how it's used in the broader context of type systems.
-
Re: difference between polymorphism and overloading
Are those really your professional ideas? Awesome, Wow!
Simply Brilliant. Your nothing but a common Troll.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
ahoodin
Are those really your professional ideas? Awesome, Wow!
Simply Brilliant. Your nothing but a common
Troll.
Mind your manners. Use arguments not defamating comments.
It's not an idea, it's an observation. This is very typical,
http://www.cplusplus.com/doc/tutorial/polymorphism/
I quote,
"One of the key features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. Polymorphism is the art of taking advantage of this simple but powerful and versatile feature, that brings Object Oriented Methodologies to its full potential."
My emphasis. Sometimes also the mechanism of overloading is included in the concept of polymorphism and contrasted with the above and you get the "static vs. dynamic" distinction.
Ask a C++ programmer about polymorphism and the above is the answer you're most likely to get. I'm not saying it's wrong or bad. But it's a limited view of what constitutes polymorphism.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by nuzzle
Some go even further and consider polymorphism to be synonymous with the C++ virtual method dispatch mechanism
This is because of the c++ standard. The only type of polymorphic behavior that goes by its direct effect is class polymorphism
Quote:
Originally Posted by nuzzle
but that won't change the facts. The predominant usage of the term polymorphism in the C++ community is limited compared with how it's used in the broader context of type systems.
That is because c++ doesn't have another terminology for class polymorphism and therefore is typically referred to as polymorphism in general. I surely know how to use other types of polymorphism. The only thing I have to know is the c++ terminology when dealing with c++. So I go by c++ terminology like type casting since this is what the c++ standard goes by and not polymorphic coercion. So yes I know polymorphic coercion, inclusion polymorphism( aka subtype ), ad-hoc polymorphism, parametric polymorphism and whatever else is polymorphic in c++ by its terminology in c++.
Quote:
Originally Posted by nuzzle
I quote,
"One of the key features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. Polymorphism is the art of taking advantage of this simple but powerful and versatile feature, that brings Object Oriented Methodologies to its full potential."
..................
Ask the average C++ programmer about polymorphism and the above is the answer you get. I'm not saying it's wrong. But it's a limited view of what constitutes polymorphism.
c++ is oop and that is what c++ mandates as polymorphism directly
Quote:
Originally Posted by c++11 10.3
Virtual functions support dynamic binding and object-oriented programming. A class that declares or
inherits a virtual function is called a polymorphic class.
So it does make since to use c++ terminology directly and not necessary how polymorphism applies to every part of the language in general.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
Joeman
So it does make since to use c++ terminology directly and not necessary how polymorphism applies to every part of the language in general.
Sure. Other languages do the same.
But the OP found it confusing and I thought he may benefit from the broader picture and not just the common C++ view only.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by nuzzle
It's not an idea, it's an observation.
I observe that my post #12 and Lindley's post #13 did not get ahoodin so riled up, probably because I used "we (...) in C++", Lindley spoke about "C++", and we made objective statements about what is the norm with respect to C++, rather than use a phrase like "quite limited outlook" to contrast with "broader view" that can be intepreted as an aspersion on "C++ programmers" as a group from which you are trying to dissociate yourself. I doubt that these were your intentions, but phrased as such, I can see why ahoodin felt you were trolling.
Quote:
Originally Posted by nuzzle
I thought he may benefit from the broader picture and not just the common C++ view only.
I agree.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
ahoodin
[directed @ nuzzle] You think that you can just blurt anything out about C++ programmers.
Lol. From my experience, nuzzle has a habit of defending C++, funny that you now put him in the "other camp". XD
Quote:
Originally Posted by
ahoodin
Must be having another senior moment.
Sure, we've all seen that nuzzle has his moments, but I think you're just overreacting to his comment - which wasn't particularly offensive, or ill-tempered. As far as I know nuzzle is a C++ programmer himself, at least among other things.
It's good to have a more brother view of things, and specifically in this case, we all can agree that the idea of polymorphism is not something language-specific.
Yes, you may never need to deal with it outside the context of C++, but it is important to make the distinction between what it fundamentally is, and how it manifests itself in the language mechanisms of C++. Then you can realize that it doesn't have to stop there, and that having this broader view will make you a better programmer, and it just might come in handy some day, when thinking out of the box is required.
-
Re: difference between polymorphism and overloading
I agree knowing how things work and why is important. That is why I like computers in the first place :) They are quite complicated.
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
laserlight
I can see why ahoodin felt you were trolling.
Sure, even in the most polite and correct C++ professional lurks a hooligan ready to strike out on the slightest hint his subculture is attacked.
Help, I did it again didn't I :)
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by nuzzle
Help, I did it again didn't I
Don't blame me if ahoodin zaps you with a laser, or more likely polymorphs you into a sheep :p
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
nuzzle
Sure, even in the most polite and correct C++ professional lurks a hooligan ready to strike out on the slightest hint his subculture is attacked.
Help, I did it again didn't I :)
That was senior moment 1736. You might want to keep your day job because standup just isn't your thing. :D
-
Re: difference between polymorphism and overloading
Quote:
Originally Posted by
ahoodin
That was senior moment 1736. You might want to keep your day job because standup just isn't your thing. :D
I think you've got it wrong. A senior moment is not the inability to get it to standup.
-
Re: difference between polymorphism and overloading
Oh well too bad you cant have that so called expanded mindset and be professional at the same time. Your just stuck talking about your dating life and your awesome performance. Good luck and maybe you will be more professional in the future.