• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

hOW IS POLYmorphism useful ?

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ??
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Fred that was helpful.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 77939
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 77939
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome And thanks to Ernest too.
reply
    Bookmark Topic Watch Topic
  • New Topic