N
N
NoMoneyException2016-07-15 13:28:37
Java
NoMoneyException, 2016-07-15 13:28:37

Is it correct to create a collection of interface type?

If I have a Car interface, and Mazda and Audi implement it (hidden advertising). Is the expression correct List<Car> list = new LinkedList<>();
or should I use an abstract/ordinary class to implement polymorphism?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fat Lorrie, 2016-07-15
@Free_ze

Everything depends on the task. If you need a local array, then you can at least implement it. If this is a container where polymorphism is really needed, then it is better to increase the abstraction as much as possible. Those. if the interface is enough for you, then you should use it. If not, an abstract class. If not, the parent implementation. If it is not enough - a concrete class.
This is how it is done in Java. But you pay for a polymorphic call with runtime brakes. Therefore, without much sense, this should be avoided.

I
IceJOKER, 2016-07-15
@IceJOKER

The main thing is to choose the right type -> LinkedList || ArrayList || HashSet ..
And the rest is not so important (relatively), but the advantage of this method is that you can easily change, in this case, LinkedList to another class that implements List

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question