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