• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between these ArrayList declarations?  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example



why is List added before <Student> since ArrayList already implements the List interface by default. Just seems redundant. Why not make it like this:



I understand you may want to make a variable of an interface type but I don't understand why classes that already implement an interface in the Java API do this. Maybe I'm thinking of this the wrong way. Any clarification of this would be appreciated. Thank you coderanch.

edit: Is it because if I decide later I want to change value of the variable( to linkedlist or vector) I wouldn't be able since it was declared it of type ArrayList<> instead of List<> ?
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Matthew wrote:I understand you may want to make a variable of an interface type

Then you understand why the variable should be List and not ArrayList.

So I'm not sure what it is that you do not understand.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're exactly right. You declare the variable as a List when your code does not care what type of List is used. If you declare as an Arraylist it's not as easy to change the specific type of List later.
 
Eric Matthew
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Eric Matthew wrote:I understand you may want to make a variable of an interface type

Then you understand why the variable should be List and not ArrayList.

So I'm not sure what it is that you do not understand.


I think my confusion arises from tutorials and code examples that do it this way when the ArrayList they're making is only going to be used only for one purpose. If there's no intention to change it later, why not just make List<> to ArrayList<>. But I think I fully grok it. Thank you.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you may have a misunderstanding of what the Interface is doing though, since you say you understand that it is good to declare variables as an interface but don't understand why you would do that for classes that implement the interface.

You can only declare a variable as an Interface type IF the class of the object you assign to it implements that interface. Declaring the variable as an Interface type does not change anything about the actual object you assign to it.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In trivial examples it probably makes no difference whether you declare the variable as a List or an Arraylist, but its good to get into good habits.

Where it really becomes important is when you write methods that take parameters or return values. When you write a method you might not know which code will call it, and it is much more flexible if you don't restrict the types that are passed in or returned unecessarily.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!