Reason The Liskov substitution principle . Directly quoted what was said.
Let q (x) be a property provable about objects x of type T. Then q (y) must be provable for objects y of type S, where S is a subtype of T.
Simple explanation
Map is an interface- And
interface is a contract
By using Map we confirm that we are using a contract of what Map should do.
for instance
doSomething(Map<String, String> map)doSomething(TreeMap<String, String> map)
doSomething(Map<String, String> map) allows you to use any implementation that confirms the Map contract, which allows you to work with any of the subtypes.
Does doSomething(TreeMap<String, String> map) accept any subtype of TreeMap . But TreeMap not a contract, so any subtype of TreeMap can violate its signature / behavior (methods actually). Thus, making TreeMap less preferred than Map . An inheritance composition can give you more information about this.
source share