aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Polymorphic Declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Polymorphic Declaration" Watch "Polymorphic Declaration" New topic
Author

Polymorphic Declaration

Torsten Oppermann
Ranch Hand

Joined: Dec 22, 2010
Posts: 62

Hello !

I was just wondering what is the difference or benefit of doing

and


OCPJP 6 - 93%
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

With 1 you can do something like



But that is not possible with 2.(Every time a new reference variable is required)


Java Experience
xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 133
Better to search in Google.. Standard way to creating Arraylist. You will get the answer.
micha koern
Greenhorn

Joined: Mar 25, 2011
Posts: 23
Its the same -List is just an interface.
You can use both in the same way (perhaps ArrayList has more methods wich you can use).

So if you want it to call it a benefit:

Benefit for Arraylist: you can use each method, that ArrayList offers.
Benefit for List: you can use it everywhere, where just a List is required and allow also other implementations (for example LinkedList).

But remember- an ArrayList is a List so you can treat it also like a List.
Suhrid Karthik
Ranch Hand

Joined: Aug 31, 2008
Posts: 58

The first polymorphic declaration is better. It gives you the freedom to change the list from ArrayList to a different List type such as LinkedList. For e.g. you might want to do this for performance reasons.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

First Declaration leads to Polymorphic reference... That is "List" can be ArrayList,Vector or any classes implementing List interface...

Due to this type of declaration, you can hide the concrete classes from the Client Developer point of view...

And Client Developer does not have to worry about the concrete classes since those classes meet the contract of "List" interface...

Doing with Polymorphic Declaration decreases the modification of the code and new class for feature can be added easily without affecting the parts of the program...

SCJP 6 [SCJP - Old is Gold]
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


This is one of the important thing you learn while learning Object oriented programming "Polymorphism" and in real world software programming it saves lots of maintenance time and you should apply this practice not only on variable declaration but also on method's argument type and method's return type. This is why in Factory pattern return type is SuperClass or Super Interface of what possibly could create() method returns.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Polymorphism vs Method Overloading...

Method Overloading makes methods to be defined with different arguments with the same name... But when a method is called which has overloaded versions of methods, compiler will search for a particular method among the overloaded methods and finally select a method to be called during runtime.. that is method to be called is declared in compile time itself...

Polymorphism is not related to Method Overloading ... Polymorphism which allows same entity in different behaviors...

Even in the case of Method Overloading, when a new version of method is added, the client program should also be re-written to call that new version of program matching the arguments of newer version... So, Method Overloading does not lead to the ease of code maintenance....

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphic Declaration