Regarding raising the level of ararians

In general, for decalre arrayList, we can declare, as shown below.

ArrayList Obj = new ArrayList(); 

It is right. But in our code we will not do this. Do as shown below

 List Obj = new ArrayList(); 

Why do we do this? Why is fading away?

And although Upcasting we limit its functionality. Any specific reason we declare ArrayList or LinkedList as follows?

+4
source share
6 answers

Yes - because if you do not need certain functionality that is displayed only through a specific type, it is usually recommended to refer to a more general type. Thus, if you ever decide to use another implementation, you know that you are not attached to anything specifically with the current implementation. You can change one statement later:

 List<String> list = new ArrayList<String>(); 

(say)

 List<String> list = new LinkedList<String>(); 

and know that everything will compile. Of course, the behavior can change in terms of performance, thread safety, etc., but it’s all the same.

You also declare that you do not need any members that are related to ArrayList<String> , which may be important when reading the code later.

All this is especially true when it comes to choosing return data types and parameter types. The more specific you are about the return type, the less flexibility you should change later. The more specific you are about the type of parameters, the less flexibility you provide to your subscribers.

+6
source

The fact is that ArrayList and LinkedList are used as lists. Our program logic should not rely on how they store the list, just so that they can be used to store items in an orderly way that can be accessed based on this fact.

0
source

That's not news. This is the right way to work. In fact, when you use List, it doesn't matter how it is implemented. It is important that this is a list. All methods you use are defined in the interface. The same is true for all other classes. Always try to use the interface on the left side of the assignment operator and in the interfaces you define. In this case, it will be easy to change ArrayList to LinkedList. Just change it in one place: replace new ArrayList with new LinkedList , and you're done.

Also, in most cases, you don't even need a List. if then you just iterate over the elements, just use Collection. Since the Collection interface is implemented both by lists and by sets. Therefore, in the future, if you prefer to store your items in a set, again you will have to make only one change.

0
source

The final answer can be found in

Joshua Bloch Effective Java, paragraph 52: access objects by their interfaces.

0
source

Its simple and simple - polymorphism

You program a more general or abstract class or interface type, such as List, and Java polymorphic behavior can automatically detect at runtime what the implemented object is actually defined on. Here List is the interface.

Polymorphism helps in maintenance and refactoring without much hassle. If you know Polymorphism, you will know it.

0
source
 ArrayList<String> list; list = new ArrayList<String>(); //possible list = new LinkedList<String>(); //not possible LinkedList<String> list; list = new ArrayList<String>(); //not possible list = new LinkedList<String>(); //possible 

but

 List<String> list; list = new ArrayList<String>(); //possible list = new LinkedList<String>(); //possible 

to increase this opportunity, you need to practice it really: P

0
source

All Articles