Adobe Scan Nov 15, 2021

Download as pdf
Download as pdf
You are on page 1of 24
by Generic Clas 1 A Generic Class iso class woith One Or more (ype vowialoles, Genevics: means parareetevined by pes. Paromelesia el Ayprs enables fo tyecte clatieS, interfaces , and method in whith the typeof data upon whith they operate is Speched as a Povometer «Clas, terlerface , or method that opmates ON a parametyéred type iS Cotted genic, os in gerne Clax ov genic method Sypttem foy declasing a Generic Class + Class class- Name lype - param list 7 Cll Syntax toy declaring arene to 0 generic class Class nome lyre ong -Wst > Vay -Pame = new class ~ name type = ong - tist > ( Cons—ang- vid), Cvomple program : Ir By Sienple gennie class 'T Aleve 7s 0 ky Pe Poremneter that Class Gen 21 { Toe; Il deg low of objat of type T Gen (1 0 ) ft ob- O34 4 [1 Betwn ob T get ob { yer Ob} i I Chow type of 1, Noid Show Ayr £ is "4 Ob. 4 cuyptem out pretio Cayps of TS d cuss0 5t Name (); Public class Gen Deno pubic (atic Void main C sieg C3 orgs) t 1 creck e a Gen reference fy inlegnrs, Gen t i . < tong > obs new Gene integer > (804 Job show type, J ) tA V = job. get obl>, Systern. out petin (S value 4 ) i Nay d "oc . vate og Gen object uy Shins, Gen < skiing > Sob = Hew Gen ce Stroh . Show TYPE Na Shing Sty = stv Ob. get 0, System out print ie” vatue + str), I show +ypr of 9 void show yr { System out pintia Crype of Tis “sob. get clas ie) yet nerrre ())', j Public clasr Gen Demo t Public static void main Cshing Ci orgs ) { 1 Create o Gen telerente for wntegtrs. gen < integer > i0b= new qo < integer? (54 ; Ob. Show TyPeO) ; int Vv = igb. getobC): 2 System gut pintin O° vatues"eu) ; NM create a Gey Object for Sbinos Gen £ Shing > Sloe peu Gen efing > CRN! Str Ob. Show vypee Skng SW = Shob gst cbt ; System. out pmo C* values” 4 su), use Of 5 _Genaic methods : . make Methods jaside Banani class Can \ iCall tne Class 46 nd are Abe fore , automa J 9 type Parameter a ) re '% possble to i Velotive ty -the lyPe poremelry . However, nn om © qenere method that Uses one > MOF YP declare . “the Slope of orgurrents Pr Listed to ruby OF its Own . ohare 4% AeClared , Tt alloaos Slatic as wellas 90N- Saltc Me Nhods Sysier tor a generic_trethod < tuPe- Porameter > telorn Aype method _ ney erv C porary) t y Cys Mele Yornpse | Clay Demo £ Sobe < VT > ved clsplay Wort) c System, Out. Pint Cy “get C leagy O oft Nome ()7 av); System - out Piin let ctass Ovgerne ta Le 4 publ static void main ¢ sting CJ os) display 8h," PND engineering College") 5 x Cxarnple 2: “towing example ilushates how we Can pint an away Of different {ype Using 0 Single Gerric ene thod Public Class Generic_enethod Fest t " generic me hod Pant anioy Public stotic ey od print Ancry ( €( 3 input Array} i " Pispleay amon element 5 fox t CE element ‘ wput aD) Tysem vont pint Ch pen element)» 3 4 Syste Sub print inl i Public Satic void ' main Csbing org sly TARANS SAISON voy dd 4D Integer () int Awery - C1, 2A? s , ae Double FT douide Avay f1I22 ; we ‘Wh Cc Cherotter[] choranay °f P MDE Contains”) System out pintio Caney ineget Aray , prot Amey Cint Amey), // pa 0° integer me) System out mini" Anay double Avay conlaing:"), put Away (double ono) N pus Double array System Out print 6 CC Anau Charatier Nua Contain, 4, ) print fay chat Anay)s Ht pass aC haractty onan to t: \ 6 class which js declawed wrth =the obs vad ; Keyword fs 4enow as an abshacl ‘ steart Tova TA con have abstack gq WO * Me-thpds with the badly - Km abstract class poust be declare —a with an: abstatt leeycvord! Ke mk can Yave abshact.¢ non -abksha —Ct methods: “SE tk cannot be \nstantiatedl KX at can have comstsucts A static: metho —A also ‘ x Te can have -firol meshed which wil Foe the subclass not a change the body PP she met ' €xample + abskact class shape & -abshratd void ayaw (15 4 sterd shape class ecctangle e void draw Fspstem oat TT Uo raeving Yect ang” - class crichet ettends Shapef wid HOMO F ayer cart patio [drawing cele) 5 class test nbshactoot § a partic static void ric | shiny op D4 ( ehape S= Mew evelet 5 eda); 4 5 -prspratt method in Jova © methad which 6 declared as abshad G cds Not have implementative js Tere as an abskath method . syptat te akskatt void sur bint , double Ys nw retro) Lady -g abstrav. ©? public obskach class demo - { y declare elds # Ceclare non abstract methods aretha void Aarowt akshact class hope. pubbdic abshatt wid crawl i putt wid paintt J sytem out pact lo (" Bloom") 4 eon e ee oe oD 4 Public ctatic yoid nnalo (sh S t shape ob} = new cape () 4 > wheo his Prgvars is o< pay) AVE® Compilation exrov bees HAS oe posible fo Crete an pied oF + akskath class shape it wil) 1} class Ye pre sersling custom ex ception ctass invalid Age Exception exter) acer § —on Public invodid-age Exception [shig SH) KY? Syftiag 4 4 public class “testcustor exception | Static void vatidatelint age) Hes invalid Qe exception] iFlagecinyf : —tevow New invattid hgeesception Lage 1 not 5 Veig ty ute > —lUrC~™” eiset ayster cul-pantite ("udleorer tovote"); / 4 pure static void mariolshing engit) t “hy t votidorte U3 ); 3 Catch Unvatid Age Exceptiones) 8 System oul partic” lt sug Woe cai oor) ae System oot part o(" exception occured 4 +ex)%, Syptero-out pint hn Lest tf He wele oF j y i Lee plivo MWervaw hy . gagtyre a DU exception clases ane HE OPT erephi she 4 “ation class Ho | \avo Jong exces re yo obl \ C oO —O9 lage is -the cut «10% i Ian. « 15 ex cept We whl & Class Oyen “than Ane Here is anther subela Ear which ts dived form the class Code? a. Ce Class Nes} Fy { public static wid main (shing ong C5 yt ‘int as aus: bength; jot = va Ja; sytem out: pasttol“a. bay Hut : Vf la=-1)asa/la-o); iPlas=2) £ . inte CJ- $14; cur} 24; 4 4 catch Covcaytirdes out F eourd creeptisn boureti;” 46); y Catch Ladthmetic Crception’) ¥ sytern atl praila Wwivde Hy 9 te: 4 > Output C3] s java nest try Divide ey 0 java Doe: CU jove Hest TH ore a\ Divide hyo jeu Bee, Anite ea CePEOMs Ca\s java rect Ty OF TWO a-z — wroy Sova dong, - HOY ae thee te er ceo ption® ndet oat of Loonds indexout @F Bounds ea. ceptton uk it a> FTonettance + cefemd at {he pro of the TOHearitane com mw of om all the propetior pehavivUys eq ly ing ancl acothey of cheld clas & tne proper lier Ac quistag and behaviouwy clast fiom Fnhertane represents poet child aelecttoarhtp . Clos Ic Nasr tho pevent TrA yvelationshie » abso the at Tygpu of Prnhettane * 2 Stngle — Fmnestlance Himearchial snhelbante L Malttleoet! sanerthante . Hybrid Ennertkance Matlt ple fnperitane u not suppartd ta Javer- Inbevitana = of — extencl?Mg single Single frow 4 subclass fom ringle super Clods clare Facut clay mango Cxamople + Clee Fycakt K i Shing Cait." sweet” votcl prtnt Teuste () 4 Se ster OU prtplto C Taste or + tate); \ 4 . elect! Mango extends Frat < sbing name =" Mango". votd ptntntanret) ‘ w Syste- out: proto (talarmne &1 2" 4 manne) ; y 3 class Strgle Demo: ‘ ta Cabling git )) pul stable votdl math ng Ug ‘ . mango m= nw mango() | mr. mint Taste (); ol. prot Name; 4 y ouTPUT caste 15 2 Sweet Narme u » wyangd: 4H teyecrcht cect Toherttanu ‘ extencl(ng — more tha OMe hip proces of fom stngle supe Clos, class Pract Cle Mango clan Apple fxample * clout Facatt ¢ shang Caste. Soveet "j vold print Teeste () ¢ Sysleeo oud pratt © Toute Ui" + tasted, 3 3 clay Mango «endl Prout ¢ stitng name s "Mango"; vote pint Name Q Sys. Out: petted Calame "4 ome); } j Clery App extender put Shing name = "Apple"; void pytntnlame { fy chro out printhe C'slame 75s tm cume ); CN RE EENGS CDE CORE TR FV0 x public statle vot AD pretin Cobing angi )) ¢ Mango ro: neu Mangol ; FOr pte Tauke Oj Por prtok Name (); Apple et. neve Apele i Ot pent Taster; curs prentaiame (1), ‘ 5 OUT POT faire U1 : Sweet Name Uo Mango Gaike te + Sweet Name 2 Apple. NMULTILEVEL TNHERITAAICE * tous fom amothey procus of exten Ng, tube sub Clous - | cleus pruié clas wyango x ctais Malgoutt Excirople cleus foul sing taste “Swat; vole protat Teutte () pyrtern out: pr toto (taste 4 4 Cocsle)i y Y cla Mango exteneeh Frit Shing name . "Mango. vod! pote lame ); ¢ Systeen ou: pintle Crlame tr: sname); J d cla Malgova extras Mango { int weight : 2; vatd meat weight C) Syston oul mint tn ("weeght Crs" tuveeght Dj y ] y Clan Mali level Demo ¢ public cbatte vord main (sting crgst I) L \ Molgova my = mew Wet powa: Ih pat Taukec); FOL mentnletme C); mI prunualeight () ; OUTPOT Taste Us fwet Name tft Mango Uleight thy HYBRID INHERITANCE | procs of extendtag new closes by Combloiny moe than on fom of (oheattance - Uyintd Trharlkanee whlch — combines Martttlevel and Hrevavchtcal Inneritan ce. fruit | mango papayc papaya without seed Example * ctoss Fut { . ' sheng taste 2 Suet; votd pl k re Tecste 0) tn Tate tee teak); systeensout’ ptt 3 4 clays mango ote! aad < "Man * * ge"; shing name vold prenk Name) ‘ sy ster out prtato (slarne tri" 4 name); j } . Cll — papaya extend frst ‘ sbing ame : “Papaya "s vold prtintalaned) Syston, oat pret (“Name tri" 4name); 5 y cleuf — papaycwtthoutf eed} eclendu papaya § tnt weight <5; vold prtnt Wl ight) sysleen oat’ printla CWleight U3" 4 weighb); ¥ J cleus Hybytd Demo pubic static vold main (sbtng argsl J) { mangd 1» new mango, Oe pacnt Takeo ; My patrol Named) ; Papayawelhot Seed ect = neo Peapcryci we thou eect! O's rene qoute), at. mitntalame(), al. pid Wheat; 9OTPOT Faste oF Sareck Name cro? Mango qaste ir Sureat Name oS Papaya intetght tes papltc clout t puplte fatic votd enti ( rrtogl J og) ( Linked (ut cdbtng? 1s mee Linked st eshtng> / cle add (Red )/ a saad Cc qian")! cr. add Cc" plack")) a: add Cminete’) a: add (Pink "5 Sys: oul: pointin ( "tee esting» C2 nw of fret lnted trib" +e hinted ust Linked (rt eSbing> ( Cp add (‘Red "); ¢2. add C'qren") ; tr. add (plac); . add (*pink") ; Sys out - mention’ Linked (ist 2 shing> a> aad Atl( (1); aeik of seonel (tered) (0t: new (tnkeditst ¢sOIng> C1 a a. add AN (CC); sy ster out print lo Cale Ltobed (UES +4 a) : a), b \ ' Nheridance 10 java is a Conept Aino * Ocquimes Ane pro perttet fom one chet oO Gthey clacset; “lon example the Peationmip blu tothe arc son |r Java, a class can anher! ©) atirthact { Nsaortement) A duantages * & Drewision

You might also like