List<String> someName = new ArrayList<String>();
ArrayList<String> someName = new ArrayList<String>();
- Does it impact anything on performance?
- The first one is a List of Objects and the latter one is ArrayList of Objects. Correct me if i am wrong. I got confused because ArrayList implements List Interface.
- Why do people declare like this? Does it help in any situtions.
- When i am receiving some email address from DB, what is the best way to collect it? List of eMail address Objects????
- Finally one unrelated question…. can an interface have two method names with same name and signature and same name with different signature.
The difference between the declarations is more one of style. It is preferable to declare variables using the abstract, rather than the concrete implementation, because you can change the implementation choice later without changing the variable type. For example, you might change the List to use a LinkedList instead.
If you always use the abstract type (interface or abstract class) wherever you can, especially in method signatures, the client code is free to use whatever implementation they prefer. This makes the code more flexible and easier to maintain.
This is true even of variable declarations. Consider this:
If the variable
listwas declared asArrayList, then only ArrayLists would be accepted in the constructor. This would be a poor choice: Always try to let the client code chose the implementations they want to use.Regarding you last question: Interfaces have the same restrictions for methods as classes do, so yes you can overload methods.