Java – polymorphic call

I'm a novice to Java, and I've seen it in the code in many places that my old people claim

List mylist = new arraylist(); (option 1)

replace

ArrayList myList = new ArrayList(); (option 2)

Can you tell me why people use option1 and what are the advantages?

If we use option2, will we miss any advantages or features?

Solution

The advantage of using option1 is that list mylist = new arraylist(); It is about the polymorphic behavior of methods For example, you can have a method that accepts list type parameters,

someMethod(List lst)
{
   lst.doSomething();
   //do somethng else.....
}

In this method, lst can be of type linked list, ArrayList, or copyonwritearraylist

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>