java - Why to keep interface as reference? -
this question has answer here:
- type list vs type arraylist in java 14 answers
i have observed in java programming language, code following:
list mylist = new arraylist();
why should not use following instead of above one?
arraylist mylist = new arraylist();
just avoid tight coupling. should in theory never tie implementation details, because might change, opposite interface contract, supposed stable. also, simplifies testing.
you view interface overall contract implementing classes must obey. instead, implementation-specific details may vary, how data represented internally, accessed, etc. - information you'd never want rely on.