This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
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

Joined: Mar 09, 2009
Posts: 600
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

Joined: Feb 19, 2007
Posts: 157
Have a look at this


R6i
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

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.


http://muhammadkhojaye.blogspot.com/
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
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

Joined: Apr 12, 2009
Posts: 449

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

Joined: Mar 09, 2009
Posts: 600
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

Joined: Oct 27, 2005
Posts: 19556
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: Polymorphism
 
Similar Threads
Multiple File Upload using Struts
declaration
meaning of double angle brackets(<< >>) in Generics?
Generics <?>
Class objects into ArrayList