java - Regarding arraylist upcasting -
in general decalre arraylist can declare below.
arraylist obj = new arraylist();
this correct only. in our code not this.we below
list obj = new arraylist();
why this? why upcasting ?
and while upcasting restricting functionality. specific reason declare arraylist or linkedlist this?
yes - because unless need specific functionality exposed via concrete type, it's idea refer more general type. way, if ever decide use different implementation, know you're not tied specific current implementation. can later change single statement:
list<string> list = new arraylist<string>();
to (say)
list<string> list = new linkedlist<string>();
and know still compile. of course behaviour can change in terms of performance, thread safety etc - case anyway.
you're expressing don't need members specific arraylist<string>
, can important when reading code later.
all of particularly relevant when comes picking return type , parameter types of methods. more specific return type, less flexibility have change implementation later. more specific parameter type, less flexibility give callers.
Comments
Post a Comment