• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Polymorphism

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might seem a very stupid question but i'm really confused.
wht is the use of declaring something polymorphically.. eg what adavantages would i reap if i say List f=new ArrayList() instead of ArrayList f= new Arraylist(). at runtime as it the actual object it refers to would be used.
Please help me with this... if it is a very basic and simple question, i do apologize before hand.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stanley:

That's not polymorphism, that's programming against an interface. Polymorphism would be something like this:

In my class definition, I have three versions of my (poorly named) doThis() method which can do different things based on the type (A, B, or C). That is an example of polymorphism.

John.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John de Michele wrote:That's not polymorphism, that's programming against an interface. Polymorphism would be something like this:

In my class definition, I have three versions of my (poorly named) doThis() method which can do different things based on the type (A, B, or C). That is an example of polymorphism.



That's method overloading what you have shown in the code.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John de Michele wrote:
I have three versions of my (poorly named) doThis() method which can do different things based on the type (A, B, or C). That is an example of polymorphism.
John.


No. Its not polymorphism.
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doing List f = new ArrayList() forces you to use limit
the use of ArrayList to the contract defined by List.


For example you may use all methods defined in the List
interface but you cannot use trimToSize() defined in
the ArrayList class
(Unless you cast the List reference to an ArrayList reference).

This allows one to "easily" use a different implementation of
the List interface either programmatically or in
some cases by simply changing code to create an object
of the other implementation of the List interface.


For simply an example..




The two implementations have the same contract when
used through a List reference, but will behave differently.

A more clear example is Using a HashMap for a trivial map functionality,
then a TreeMap when you would like the keys to be in sorted order or
a LinkedHashMap when you would like the keys to be in
the order you inserted them, using a "Map" reference.

 
Muhammad Khojaye
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Walker wrote:what adavantages would i reap if i say List f=new ArrayList() instead of ArrayList f= new Arraylist().


same discuss here

 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muhammad Ali Khojaye wrote:No. Its not polymorphism.



Muhammad:

Have a look here. Clearly, there are others besides myself who share the view that overloading is (one aspect of) polymorphism.

John.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And most of us disagree. Polymorphism is achieved by extending classes and overriding methods and/or implementing interfaces and their methods. Overloading is perhaps sometimes called compile-time polymorphism, but that's not "real" polymorphism.
 
reply
    Bookmark Topic Watch Topic
  • New Topic