List list = new arraylist () How to Understand ???

Source: Internet
Author: User
1. List is the interface and arraylist is the implementation class of list.

This is a downward Java transformation and polymorphism. The parent class references the Child class object.

 
List is an interface that cannot be instantiated (an interface is an abstract class). Therefore, it must be instantiated using its implementation class ..

2. The following reasons are involved in writing list = new arraylist () instead of arraylist = new arraylist:

1. What are the benefits of interfaces? What are the benefits of this definition method?

Of course, you can use arraylist list = new arraylist ()
But it is generally not used this way.

2. There is a dependency inversion principle in the design model. Programs should be abstract-independent.

In Java syntax, this method points to a specific implementation using interface references.
For example, if you want to replace arraylist with the implementation of 'arraylist', you only need to change one line:
List list = new vertex list ();
Other parts of the program do not need to be modified, which is flexible.

If you want to set the storage structure to a partial list, you only need to set list = new arraylist ()

Change to list = new external list (), but do not need to be changed for all others. This is also a good design model.

An interface has multiple implementations. When you want to change the implementation method, you need to make minor changes.

3. Interface-Oriented Programming

4. Improve program extensiveness and improve modification and maintenance in the future

Iii. Detailed explanation:

Arraylist does not inherit the list interface, but implements the list interface.

You can write arraylist = new arraylist (); this will not cause any problems. Compared with list = new arraylist ();, the two writes are different.

Arraylist is an arraylist object. It can use all methods of arraylist.

List is an interface that cannot be instantiated (the interface is an abstract class). Therefore, it must be instantiated using its implementation class.

Although the list object is also instantiated as an arraylist object, it is actually a list object. The list object can only use the methods in the list interface that has been implemented in arraylist,

The methods in arraylist that are not defined in the list interface cannot be accessed.

We say that using interfaces is advantageous. If you define a type as an arraylist (that is, a specific implementation class) then you can only receive this type of data. If you define it as a list, you can not only receive the arraylist object but also the List objects, in this way, your program becomes flexible.

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.