Refer to objects by thier interfaces. Always?
A cut code from effective java, here we used a List to obey the good
practice of refering to objects by their interface.
// Good - uses interface as type
List<Subscriber> subscribers = new Vector<Subscriber>();
Assume, that we had a "car" interface and "2wheel" and "4wheel" were
concrete subclasses. Is it even recommended ( seems quite a yes ) to
construct a list of type "car" ?
List<Car> car = new Vector<2wheel>();
instead of
List<2wheel> car = new Vector<2wheel>();
No comments:
Post a Comment