|
-
October 14th, 2011, 03:39 PM
#1
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.
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.
-
October 14th, 2011, 03:45 PM
#2
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.
-
October 14th, 2011, 03:47 PM
#3
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
int Check(Int val1, Int Val2){}
when you write:
Because of the number and type of parameters passed to the function.
ahoodin
To keep the plot moving, that's why.

-
October 14th, 2011, 10:24 PM
#4
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
-
October 14th, 2011, 10:36 PM
#5
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.
-
October 14th, 2011, 10:48 PM
#6
Re: difference between polymorphism and overloading
Thank you, Lindley, ahoodin.
 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
-
October 15th, 2011, 02:29 AM
#7
Re: difference between polymorphism and overloading
 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.
-
October 15th, 2011, 08:20 AM
#8
Re: difference between polymorphism and overloading
 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.
-
October 15th, 2011, 12:23 PM
#9
Re: difference between polymorphism and overloading
 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.
-
October 15th, 2011, 12:57 PM
#10
Re: difference between polymorphism and overloading
 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.
Last edited by nuzzle; October 16th, 2011 at 11:51 PM.
-
October 15th, 2011, 01:17 PM
#11
Re: difference between polymorphism and overloading
 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.
-
October 15th, 2011, 01:38 PM
#12
Re: difference between polymorphism and overloading
 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:
 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.
-
October 16th, 2011, 10:02 AM
#13
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
-
October 17th, 2011, 07:23 PM
#14
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
-
October 19th, 2011, 01:16 AM
#15
Re: difference between polymorphism and overloading
 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.
Last edited by nuzzle; October 19th, 2011 at 01:25 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|