-1

Usually declaration type is interface type and initialization part has implementation type. what is differnce between List<String> list = new ArrayList<String> and ArrayList<String> list = new ArrayList<String>? What is difference beside polymorphism?

1
  • 2
    Hint: it took me less than 10 seconds to find this answer. Next time, please you try to do that prior research. Commented Dec 23, 2016 at 9:32

2 Answers 2

1

There are no important differences. But if you use something like this

void doSomething(List list)
{}

you can use any object which extends the List class, but if you use something like this void doSomething(ArrayList list) {} you can use only ArrayList object and its subclasses.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, is there any easyness in terms of code edit in future if i use Interface(eg: List<String> list = new ArrayList<String>();
Yes, abstraction is one of the OOP's principles!
can you give me an example? ;)
Now, your list may be object of the ArrayList class, but lately it may be object of the LinkedList class. Read about Polymorphism to understand it better.
0

List is an interface and ArrayList is its implimentation class. We can't create object of interface because they are abstract but we can create reference of interface which is nothing but List list . Using this reference we can invoke methods of ArrayList.

1 Comment

This doesn't answer the question. It doesn't answer the differences between using List<..> or ArrayList<..> as variable type.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.