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

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -