How to Understand: List list=new ArrayList (); Why declare a list instead of a ArrayList ?

Source: Internet
Author: User

Using interface-oriented programming in Java, which is for abstract programming, is a better way to reduce coupling ArrayList LinkedList is a sub-class of list, like Set is also collection sub-interface its implementation has HashSet LINKEDH Ashset TreeSet, etc.

Many times in the work, we need to put many kinds of instance objects, all thrown into a set, this time we should find a standard, interface is a standard. Or it can be understood that many kinds of subclass instance objects are thrown to the collection that holds the parent class instance.

Move up: Subclass objects are missing methods that are different from the parent class. Missing subclass-specific methods

Down transformation: You can call the subclass of those unique methods, regain the missing subclass-specific methods < generics are common >

The first form uses the current class as a reference type, so you can access all the common methods in the ArrayList class. The second form, the interface list implemented with ArrayList as the reference type, can be accessed through a list reference to the method defined in the interface. That is to say, ArrayList this class implements the list interface, in addition to implementing the method declared in the interface list, additional methods can be implemented. However, the second form is not able to invoke a method other than the list interface.
In use, the design pattern is: "The code is as dependent on abstraction as possible, not on specifics". The first form is dependent on the specific, the second form is dependent on abstraction. Because list is an interface. The benefit of code that relies on abstraction is that the code can be easily replaced. For example, the code list List = new ArrayList (); The collection is manipulated by list. After the code is written to find that the collection is not used accurately, you should use LinkedList, so long as you modify a line of code list = new LinkedList (); Yes, this line of code does not need to be modified, because the list interface guarantees that the calls are all methods in the interface. The ArrayList and LinkedList both implement the list interface. And if you use ArrayList list = new ArrayList () in this form, then the list can be accessed by a method that is unique to the ArrayList rather than the list interface. It is possible to change a lot of code when you replace it with LinkedList.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.