Save India From Corruption - Anna Hazare.
Save India From Corruption - Anna Hazare.
SCJP 1.5(97%) My Blog
shivendra tripathi wrote:Hi Ravi,
First of all overloading is not polymorphism. Only overriding is polymorphism. Oveloaded methods is totally different method having only one similarity that is name. I would like to give an example where polymorphism is useful.
List list = new ArrayList();
now suppose you have used list in your code and later on you want to use some data structure other than ArrayList. Then you have to modify just one line
List list = new MyList(); your application code is still valid. So this is the power of polymorphism you can change the implemetation without changing your application code. Now at runtime MyList will be picked instead of ArrayList
Fred Hamilton wrote:Polymorphism allows you to do that, but it allows other things too, do you agree?
Ernest Friedman-Hill wrote:
Fred Hamilton wrote:Polymorphism allows you to do that, but it allows other things too, do you agree?
That's pretty much the definition of polymorphism: you call a method, and one of multiple existing method implementations is executed depending on some conditions.
Regarding whether "overloading" is a form of polymorphism: some people call overloading compile-time polymorphism or static polymorphism, and overriding runtime polymorphism or dynamic polymorphism. These terms reflect the fact that overloaded methods are chosen by the compiler, while overridden methods are found at runtime by the JVM itself. I don't especially like the "compile-time polymorphism" label myself, but I think it's important to understand what it means when someone uses it.
Now, to speak to the original question: if someone asks you why something is useful, you don't define it; they presumably already understand what it is. They want to know why one would use it. If you asked me "what use is polymorphism," I'd say it improves encapsulation and modularity (by hiding implementation class names, and grouping related code together), simplifies code (by breaking up chains of it-then statements) and improves performance (by using low-level JVM mechanisms rather then those if-then statements.)
Campbell Ritchie wrote:As EFH says, some people say "compile-time polymorphism". Like EFH, I dislike that term. It probably varies from language to language, but in Java the only members which can support dynamic binding are instance methods. So polymorphism in other languages might allow access to other members, but they way I like the term, in Java, it means access to different types of overridden methods.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |