Java Newbie here
New instances are stored in variables. However, I see sometimes, that the type of the variable is different from the reference type of the object.
Y y = new x
Why do programmers do this?
Java Newbie here
New instances are stored in variables. However, I see sometimes, that the type of the variable is different from the reference type of the object.
Y y = new x
Why do programmers do this?
Because sometimes you have common interface for different implementations. For example
List<String> exampleList = new ArrayList<>();
and the other one
List<String> exampleList = new LinkedList<>();
Interface List
provide for us the same methods for this two implementations, but how this methods works depends on implementation. Welcome
Most time Y in your example is an interface so you can change later the Implementation without to change the rest of your application