• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism Instantiation  RSS feed

 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)
I did my in code like this.


2)
but i have seen people doing instantiation like this also-


Compiler doesn't complains about it.

So i am using the 1st method, there should be no problem.
Am i right ?
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Until you decide that a LinkedList is a better solution, and you call some methods of ArrayList that aren't part of the List interface like ensureCapacity. By declaring as List, you will make sure that you can substitute it for all List implementations if needed.
 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Rob

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)

from above statement-
looks like ArrayList implments all List interface methods that i currently need, so ArrayList type should not be a problem.
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you have it backwards. you should already know that ArrayList implements all of the List interface methods you need, because it implements all of the List interface methods. The question is, are there ArrayList methods which you need, which do NOT implement something from the List interface? That would be the only reason to use an ArrayList variable. Generally you should use a List variable in a case like this, for the reasons which Rob already gave.
 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got you Paul,

anyway i am using ArrayList type, though i do not need any special method from ArrayList. Still i am using it.
Nobody can say that, i did something wrong coding ?
if yes, do i need to change type back to List immediately.

As perfomance wise and coding style wise, it should not make much difference.

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amandeep Singh wrote:As perfomance wise and coding style wise, it should not make much difference.



Performance wise it should make almost no difference, and it certainly won't be slower. Style wise I would say that using the List reference type is a more common idiom than using the more specific ArrayList reference type. For many use cases using an Iterable reference type will suffice. I would say that it's more important that you try to use a more generic type when returning an object from a method, or declaring a parameter to a method. It offers much more flexibility.
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's called coding to interfaces. Its the best practice. You can easily change your array list to a linked list if you handle your object using a reference of type list.
 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can eaasily change the type also.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!