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

Polymorphic Declaration

 
Torsten Oppermann
Ranch Hand
Posts: 62
Android Java PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello !

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

and
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With 1 you can do something like



But that is not possible with 2.(Every time a new reference variable is required)
 
xsunil kumar
Ranch Hand
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better to search in Google.. Standard way to creating Arraylist. You will get the answer.
 
micha koern
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
Eclipse IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic