Overloading,as,Polymorphism,and,misc.,topics,COMP,401,,Spring,2013,Lecture,5,1/24/2013,Today,• Constructor,and,Method,Overloading,– Our,first,example,of,polymorphism,• RevisiHng,StaHc,Class,Fields,and,Methods,• EnumeraHons,• Assignment,2,sHll,coming,Polymorphi sm,• Poly,=,many,,morph,=,forms,• General,principle,of,providing,access,to,an,abstracHon,or,method,in,many,forms,– Idea,is,that,different,forms,“fit”,different,contexts,– Note:,underlying,funcHonality,is,the,same.,• In,OO,programming,,principle,is,evident,in,a,number,of,different,places.,– Constructor,overloading,– Method,overloading,Constructors,• What,happens,when,you,don’t,define,a,constructor.,– Default,constructor,with,no,arguments.,• Creates,new,object,with,all,fields,set,to,default,value,– Numeric,fields,set,to,0,– Boolean,fields,set,to,false,– String,,Array,,and,any,other,sort,of,reference,value,field,set,to,null.,• lec5.v01,Constructor,Overloading,• Can,define,mulHple,versions,of,the,constructor.,– DisHnguished,from,each,other,by,type,and,number,of,parameters,• Must,be,some,difference,otherwise,the,compiler,won’t,be,able,to,tell,them,apart.,– When,you,use,the,constructor,,the,right,one,will,be,chosen,based,on,the,parameters,provided.,– Note,that,if,you,sHll,want,a,default,no[argument,constructor,,you,have,to,provide,it,explicitly.,• lec5.v02,Constructor,Chaining,• Common,pa\ern,is,to,“chain”,one,constructor,off,of,another.,– First,line,of,code,in,the,constructor,must,be,the,this,keyword,as,a,funcHon,with,parameters,– Matching,constructor,is,called,first,and,allowed,to,execute.,– Then,remaining,code,in,original,constructor,called.,– Can,chain,mulHple,constructors,one,on,to,another,• lec5.v03,Method,Overloading,• Regular,methods,can,also,be,overloaded,– Same,method,name,defined,more,than,once.,• Return,type,must,be,the,same.,• Method,type,must,be,the,same.,– Instance,method,or,staHc,class,method,• Parameter,list,must,somehow,be,different,– Again,,this,is,how,the,compiler,knows,which,one,is,meant.,– Either,different,in,number,or,type,(or,both),• One,version,can,call,another,– No,restricHons,on,when,– No,special,syntax,• lec5.v04,,lec5.v05,Why,Overload?,• Provides,access,to,constructor,/,method,in,a,more,context,specific,way.,• LimitaHons,of,overloading,– Does,not,handle,the,case,when,you,have,two,different,situaHons,that,aren’t,disHnguished,by,the,number,or,type,of,parameters,being,passed.,StaHc,Class,Fields,• Indicated,by,sta&c,modifier,to,field,declaraHon.,• Can,be,accessed,via,the,class,name,– access,modifiers,can,also,be,used,to,control,visibility,• Contrast,with,instance,fields,– Each,object,has,its,own,copy,of,an,instance,field,• Value,of,the,instance,fields,are,the,state,of,the,object,– Only,one,copy,of,the,class,field,• Associated,with,the,class,as,a,whole,• Common,uses,– As,named,constant,values,associated,with,the,class,• In,this,case,,modify,the,declaraHon,of,the,field,with,the,keyword,final,• By,convenHon,,named,constants,are,ALL_CAPS,– As,collecHve,informaHon,associated,with,the,class,as,a,whole.,• Avoid,accessing,staHc,class,fields,through,an,object.,– It,does,work,,but,it,is,misleading,and,not,good,style,• lec5.v06,StaHc,Class,Methods,• Methods,associated,with,the,class,but,that,are,not,associated,with,a,specific,object.,– No,this,reference,within,the,funcHon.,• Common,uses,– Ge\ers,/,Se\ers,for,staHc,class,fields,– Helper,/,auxiliary,funcHons,– Factory,methods,for,creaHng,new,instances,• We’ll,revisit,this,in,more,detail,later.,• lec5.v07,MoHvaHng,EnumeraHons,• Ofen,need,to,model,part,of,an,object,as,one,value,from,a,set,of,finite,choices,– Examples:,,• Suite,of,a,playing,card,• Day,of,week,• DirecHons,of,a,compass ,,• One,approach,is,to,use,named,constants,– lec5.v08,• Drawbacks,of,this,approach,– No,type,safety,– No,value,safety,,Simple,Java,EnumeraHons,• General,syntax:,access_type enum EnumName {symbol, symbol, ...};!• Example:,– public,enum,Genre,{POP,,RAP,,JAZZ,,INDIE,,CLASSICAL},• EnumeraHon,name,acts,as,the,data,type,for,the,enumerated,values.,– Enumerated,values,available,as,EnumName.symbol3,as,in:,Genre.POP,• Outside,of,the,class,– Fully,qualified,name,required,as,in:,Song.Genre.POP,• Symbol,names,don’t,have,to,all,caps,,but,that,is,tradiHonal,• lec5.v09,Not,so,simple,enumeraHons,,• Java,enumeraHons,are,actually,much,more,powerful,than,this.,•
View Full Document