开发者

Regarding arraylist upcasting

In general to decalre the arrayList we can declar开发者_Go百科e as below.

ArrayList  Obj =  new ArrayList();

This is correct only. But in our code we will not do like this.we do as below

List Obj = new ArrayList();

Why we will do like this? Why Upcasting ?

And While Upcasting we are restricting its functionality. Any specific reason we declare ArrayList or LinkedList like this?


Yes - because unless you need the specific functionality only exposed via the concrete type, it's generally a good idea to refer to the more general type. That way, if you ever decide to use a different implementation, you know that you're not tied to anything specific to the current implementation. You can later change the single statement:

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

to (say)

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

and know that everything will still compile. Of course the behaviour can change in terms of performance, thread safety etc - but that would be the case anyway.

You're also expressing that you don't need any members which are specific to ArrayList<String>, which can be important when reading the code later.

All of this is particularly relevant when it comes to picking the return type and parameter types of methods. The more specific you are about a return type, the less flexibility you have to change the implementation later. The more specific you are about a parameter type, the less flexibility you give your callers.


The point is that ArrayList and LinkedList are both used as lists. Our program logic shouldn't rely on how they store the list, just that they can be used to store items in an ordered way, which can be accessed based on that fact.


It is not upcasting. It is the right way to work. Actually when you are using List it does not matter how is it implemented. It is important that it is list. All method you are using are defined in interface. The same is correct for all other classes. Always try to use interface in the left side of assignment operator and in interfaces you define. In this case it will be easy to change ArrayList to LinkedList. Just change it in one place: replace new ArrayList by new LinkedList and you are done.

Moreover in most cases you even do not need List. if then you only iterate over the elements it is enough to use Collection. Because Collection interface is implemented by both lists and sets. So in future if you will prefer to store your elements in set you will again have to perform only one change.


The definitive answer can be found in

Joshua Bloch's Effective Java, Item 52: Refer to objects by their interfaces.


Its plain and simple - Polymorphism

You program to a more general or abstract class or interface type like List, and Java's polymorphic behavior will be able to automatically find out at runtime what actual definiton the implemented object belongs to. Here List is an interface.

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


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 possibility u need to practice this actually :P


Example and use with below example :-

public static List<Integer> intList;

public static List<Integer> ArrayListDemo() {

    intList = new ArrayList<>();
    intList.add(100);
    intList.add(200);
    intList.add(500);
    return intList;
    
}

public static List<Integer> LinkedListDemo() {
    intList = new LinkedList<>();
    intList.add(10);
    intList.add(20);
    intList.add(50);
    return intList;
}

public static void main(String[] args) {
    System.out.println(ArrayListDemo());
    System.out.println(LinkedListDemo());
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜