Not sure if my title is specific enough. Let me show you this:
class D {
private List list;
D() {
this.list = new LinkedList();
}
public LinkedList addHello() {
this.add("Hello");
return this.list;
}
}
I get it that since LinkedList implements List that I can use a LinkedList for a variable with a List type. Now I have the a method that returns "the list" after it added "Hello" to it.
I am a bit confused about it, but I guess I am right when I see two possible compile errors there right? At first, the List has no type, so how can it add a String? So it should be:
private ArrayList<String> list;
So the first compile error ist the add method, and the second WOULD be that the method addHello() returns the list which has the type List.
So my main question is: When you refer a List variable, or to say it more in general: if you have a interface, and you use it as a variable and then you refer to a class that has implemented the interface ( this.list = new LinkedList(); ), It does not mean that the List technically became a LinkedList, right? Even though the List refers to a LinkedList it does not make it one, am I right?
So the method returns a List but it demands a LinkedList.
Is that all correct?