0% found this document useful (0 votes)
33 views15 pages

Non Primitive Typecasting

java NonPrimitiveTypecasting (1)

Uploaded by

2004213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
33 views15 pages

Non Primitive Typecasting

java NonPrimitiveTypecasting (1)

Uploaded by

2004213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 15
NON- pemmitive Tyee CASTING - ———— 1 + “The process oF Comet ea ors primitive type @netmer — non-parnttive type wee cat it as Non- Pprmedive *yPe eons Non: pyimitive tyre. casting fs not possible netthes mplictttey nor epee for the etasses cwtiich( doesn't rave ong rHationship biee them 2 : > class Driver + 4 Public statte vole maim Cstrng £3 a a a A obj= eco biO> Bobj=—nrewe SEY } 6 obji= obj j Nere implicit conversion not 3 poseille. class Driver? puctic static voto mare Cstring 11 age? 4 A objs new AC)5 B obja= (er obj/ dcr Nore: Wwe can convert One Neon: primitive type +o anower Non- primitive oP onty when we have & lek relationship bexween 4wo classes “the Non primitive type casting ts peel te ine below situations: ) Lhenever we howe leh relationship beteseen -wo ctasses Lohenever we dave 3 2? common — ctrild-[Meuti ple Anvre niteance int g te105 3 Class B extends A au int GR = 205 3 class Drivers t PeblUc static void main Cstring ci args) L & obj: new Bc; Sgplers Cut: bin ten Cobj- \)5 Saplees out printin Covj-2)5 a ob ja = ebjs me pectoe™ “prokte Cobj) ; SGM printts Cony ay, System ok prints Cobja.4); , Sf: Cctiprinktny Cobjt- fs Jere 3 3 Do the avove ample we aye Sorversing one non. veference type I pumitive hype to ANolh ew Wherence] Ron pamitive fy pe bey we have ls-a relationship between bork the classet the Mon prinnih ve Aype casting can be Classified into -tewo types 5 > vp casting 28 Decor cash bee casnngq: The preset of centerting sus-clase. reperene Aqpe tote Sepers class vefenence teype fe povon Uprensting. [etutd te parent conversion] + Vpcase aa es aise Emown as Auto class & 4a lot 42202 4 clase Ay Pablic static vera a Deiver 2 man Cewring 63 area) e&j= neces Bc; obits bls Hers Cp easing ~ dene Soopers ebjee : 7 J 2 METS = opcmsting ~ clone implant Deco BoD wee wrens / onsen ee ee we “Pp cast One 7 : eho) alot AYE Se et nee ek cee eee, ctnte mentors —vettn ays betp oy fuper clous wefevince sy class 8 ‘extends A a i class pavers i puble static vole 1 main (sting (1 args > © ovj= new #095 Sy item oak prinktn (obj) Sythe out priotte (obj: A objs= obj: System: ect: printtn (obj); Sipsieuns ok priotte Cony 4): Atysletas cot printte Conj.t. 1); — eck prinkts (ob)4-j); //ere a) NOTE: 6451 OTE: Asem cut Prtottn CobJt. J) - In ine above pgm , we wit have compile time error instreection corHen aboee, wolich can be explained cong > Wtkh athe S555 555 5 5.5 5 = wiaeaeteereote > Geer i class B “extends 4 i int j= 205 4 class A 4 int is 105 D s class Divers i Peete crag. main (string 63 ar: 1 ? & bj new Bc); Satie. oak ade XO ipys Sapper: Onk Printtn (obj Js A obj1: obj 5 Siler: ont. printty (oe); Spica ot Print Con), Nother ook prtotte Conja. Sgptenu Oot Pri nhl Couja 4 Ds. “po Vere y NOTB. Ouk: pai jas ate qe es Prinkin Cooja p — Sn Ame above Pm we ast "e compile 4, | TOY because of se hie inetreection — covitien ee *2plaineey poe i Oe Of the fottoevin, With Abe het, Pt Semantics =. LL Srrtice obji-j Semantics ceceeing 4 cleore” example, WIL chean whebber the ~vasiatle te present tnotde obs Ayre fe ctase A. ametcle eee tee aera J orether clectared nor Tohemted, dtence we Gr Compile time Error. 2> hh the heey ed Se Sf Super class Namatles .: b9Ihenewer we have reference vwanriables created ty for a clase fs fnvelvecdt en 1s-A Wetakfonship eit other class , that reference vaniatl wth hace the wisibibty ont to te members af tle class as wet as ths Seaper etass. De wit not be able +o see the members Cf fs ea class. 8 @ wo -- 7] Members of object i class 4 eee omens ap apes] CHEETA ype OF Sup ctass tte F Apvantage of UpcasniNgi ’ Ui eating hetps 0s te create a generatisec! container £4. class Banke cu 3 Ineide etic we can store ang ef tne cuts class wefrences Sp oe centr tne cele havnt a ean Betoco:- Ce eal eee oe bene eee eee ee Accounls inside tk. ie Bante Object and account | = TT -ine ctass bark we need +0 create a container camted as OL which of aype accounts 4 pccounts os lliys |] inarivg reference Variable sh ass Account lass SA entends = Aecorsnts aad name> SAOEI- Sa (string name) t Ahis s names name: 4 class CA extends Accounts ; = 4 class La 4 5 class priver 3 t ettends Accounls Public staste vold mein Csering ang) ae Bank obf: new Banel)s Byes out prin ttn (obj-at)s [Joes ebf-at2 new sal" Sheeta"): a cece eat printtn (oby-at)s //sa@iot Obl at= nee CAC)S Eyes cet: printitn Cow) a4), MA ca® (02, cea, = new LAC)? Agee oe printan (ob)-84) 51 @ 103, 5 BOTE To overcome the problem oh Cpeeseg we wi ge jor Downs casting PB pownte casting + tS he process Ph Converting Super etass reference # Tape foe Sub ctass reference “ype tf Enon ae. | Dewn- casieg anes) weit never happen implicrity + use cote ‘ qe oe 2g: class i intizios 5 Class B extencs A a int jenos 3 Bisse) peveral t pubGe Static vora mein Cstri c 0903 ar : J19 args) A ebj= new BC): /Jers > opcasting B ebjt= obj 3 Vers > Boee esto. 4 3 Nore + Down case must be done huss Bis Abe operator ctype cast dass B 4 ink j2203 3 clase Drivers { Puelic static void main (string C1.arge) i A ebjs new ec): B objt= Ce) obj 5 lets. - Bowen casting. clone explieztty F 4 4 b ADVANTAGES: D The mein advantage of Pew catting 2 we soit be able 40 access tne members 0} Suciass 55 : iq: class A aps ame ink intoz z 4 clase @ 4 tat fe20s y class Driver 3 4 Pubize arate wath mato Cote teaues: A Geter atte A obfs neces ec); fe N84 steu-oeuk- printer 4 Porte (Oe 1) Hore bez cannot access ‘ Bub class member am eupercta B ob}t= (8) ob}; veperence. - Sgiiem: out priontin (on ja.t) ; \ sHvUHvVYUVUUVLUY W Deaweacee: D> Clase CastPxception : Pion Te ts a men ene preblern colic occeers dering ' Heeen cersting, 4b Abe Created object ! ane members doesnt hare the members of eub-class Note ; NOTE + yo {it Masecast fception, we mest create Seper class obpeck DYpse bere to downcas dow, © the superclass weyerence to Sub-Ctass we perencel, ‘ Eq) clase A a int f210; 4 Class B extencls 4 a tat j= 203 3 class Diver 4 a Aye pumice static vote mein (eeringt3 ar ) : ao Syflene: Ont: printtn C "Man Begins), A obf: new aC)s Popes cae poten C1), B objt = Ca) ovj : HU doconcasting Br ~ Llase Cast Ergeption Ligilen o8t- printtn (99; Spur oUF: pr arto( obj 46J); sOe can avoid Glass Cast Exception by eee shetiner the has he — Insteance ef & particatar Clase befor Operator Emoecon as * tnetanceof” ie mene Lo y instanceof + te Oo Keqeoord - Te a einen ererator ts used +o etiecte wihelfher an ee bees instance O particulary class oF not ’Crmerm bers) - The reterrn AYP] e4 Enstanceel operates ts boolean. MOTE ‘Wwe can eneoe sme instance of O. portation class get if Aime creattect — olsj ect reference type ee th Bictass 40 turin we ave shearing (@ te viog “eon relasttonstup. + Glee, we tt cre SYNTAX: Object reference Inetancem$ Clasentame = ee ol oeseet Feorr caerorT Lorre) — Favava] élass object i 5 ete tiedt ceeds object Class Carrot extends Object 4 i 5 4 class Apple extends Areete class Guava extends. rath A t 4 3 class Driver4 4 peblic static vetd main Csteing 3 args) ts Frait obj= oeew Applecrs System cae prinktn (obj instanceof Object); /) tere, Seer: out prinkte Coby Instanceof Ereeit) 3 //tere faster ou printin (obj instanceof} Apple): Ure 7 eile: prinktn (obj instanceof Guava); U forse Sgtermoust pantte Cob) instanceof - Carrot); (ore “class Drivers A pubic “static void main (string £2 args) 3 Object obj = neco Apple 03 S-o-pte (eb] Instanceof Object): true opto (obj instanceo} Feceit)s Were Ss0-plo [ob] instanceof Apple) Wire S-o-pln - (ob) Instanceofg, Guava): // forse S-o:ple (bj instanceof Carrer)s Ufatse- Apple @ 100 object pegs a jaa NEM of Object obj ! Instance ed Arie p oe Instance © { Apple | u ACEC OR URURURORORC VEU yy ot

You might also like