File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism

 
Stanley Walker
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
John de Michele
Rancher
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
M K Rayapudi
Ranch Hand
Posts: 160
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this
 
Vijitha Kumara
Bartender
Posts: 3909
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • 0
  • Mark post as helpful
  • send pies
  • 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
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic