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.