Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading vs Polymorphism

 
satyanarayana bnv
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the exact difference between Overloading and Polymorphism?
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well actually you can compare Overloading and Overriding.



The above three are overloaded methods as they have the same name but have different parameters. Overloaded methods have static polymorphism as their calls are bound at compile time. So if you call these as
add(1.2f, 3.5f); //calls 3
add(2,3); //calls 1
add(1,2,3); //calls 2




The above is overriding. method marked as 2 is overriding method marked as 1 as they have same name and parameters. Their calls are bound to appropriate methods at run-time. So they are dynamically polymorphic.
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

overloading:- method is invoked at run time on the basis of type of reference variable.
overriding:- method is invoked at run time on the basis of type of the object reference variable is refering to.

I dont think there is any term like static polymorphism in java.
Overriding is polymorphism.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bittoo garg:


I dont think there is any term like static polymorphism in java.
[/QB]


I Agree This Point
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is just called as static polymorphism. I also know that it is not polymorphism. But since the method name is same and parameters are changed, it is called static polymorphism as compiler can detect which method will be called. At least it was called static polymorphism when we used to study C++ in our school days....
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here, Ernest Friedmann-Hill says he dislikes a term, I think "compile-time polymorphism" and I think I shall take a disliking to "static polymorphism" for the same reason.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by myself:
Here, Ernest Friedmann-Hill says he dislikes a term, I think "compile-time polymorphism" and I think I shall take a disliking to "static polymorphism" for the same reason.


Bittoo Garg, I think you are right on that point.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cambpell
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by seetharaman venkatasamy:
Thanks Cambpell
You're welcome.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic