This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes hOW IS POLYmorphism useful ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hOW IS POLYmorphism useful ?" Watch "hOW IS POLYmorphism useful ?" New topic
Author

hOW IS POLYmorphism useful ?

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234



Hi


Recently when discussion with one of my collegue he asked me how is Polymorphism useful in Java . As reply to this i said him " The concept of Overloading and Overriding are of Polymorphism "

He looked at me in disgusting .

Please tell me is the answer what i answered is correct or not ??


Save India From Corruption - Anna Hazare.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
well suppose you had a superclass and several subclasses. Polymorphism allows you to have declare variables of the type of the single superclass that can contain references to objects of any of the subclasses. then you could reference (call) a method using the superclass variable, yet the actual method being called would depend on the type of the subclass object being referenced.

You could also group the subclass objects together in an arrayList of a single type, the type of the superclass.

I would say the advantage is primarily one of convenience.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thank You Fred that was helpful.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
You are welcome. It is worth adding that this idea is particularly useful , but not restricted to, your superclass is an abstract class. Even though you can't instantiate an abstract class, you can declare a reference variable of that type, and use it to reference objects of the concrete subclasses.
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
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


SCJP 1.5(97%) My Blog
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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



That's a good point that I hadn't fully appreciated, if you are using polymorphism to call a method that actually is a part of the subclass definition, it must be an overriding method.

One point I am not certain of, To me, polymorphism means a little more than just calling an overriding subclass method using a superclass reference variable. Polymorphism allows you to do that, but it allows other things too, do you agree?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.)

[Jess in Action][AskingGoodQuestions]
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.)


Maybe, maybe not. I had thought that polymorphism was the act of having a reference variable of the type of the superclass and using it to reference an object of the type of a subclass, with all that that entails. And to me that means that equating polymorphism to calling overriding methods was too limiting. So I wanted to establish consensus on the definition.

Anyways, I understand the concepts, but I get a feeling I am not quite right on the terminology, so I stand corrected.

regards.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38024
    
  22
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.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.


Thanks CR, as far as I can see your assessment is bang on. For my own purposes, I can live with my use of the terminology, such as it is, as long as I understand the concepts. But I agree that when it comes to exchanging ideas with others, a commonly accepted terminology is more important.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38024
    
  22
You're welcome And thanks to Ernest too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hOW IS POLYmorphism useful ?
 
Similar Threads
Polymorphism
How many types of polymorphisms ?
Polymorphism
never satisfied with polymorphism
MasterExam OO question