Deum my defi , Defo
Defi necw date (); Date Defi = nec datec);
tme Defa =ne co dimeC);,
Defa ne co dime ();
Defi dup c);
Def cusp
33
Super
Sapea i's a o ference Uasable which is wed do durcdlq
&efen dthe Comstrue dors o sapen tlass
class Bex
int
Box(int n q)
this ac * thui y ia
class Box? etends Boc
nt y3
Boxa ( in i n y int 2
Supen ( T, Y);
class Supen do mo
PS VM CSting aC)
BocI b =necw Boct 0 );
Boc ha = new Box3 C too , ta0, 2oo)
packaqes
Packages a condaunvs Ja class . that paclcaqes
ane Sdor ed in a huenan chical annen md ane
erpiuy
ioposted into netw class deJeoidtons Packages Com be
dodind as a named Coll ecdiom clases The pwpose
make id easg to
Loupin9 classes in a pa ckoge 13 to
add dne classu into odhea progrom ode have
We may have
gD bulc O owm clas ses fog homd ung oun data omd
Cesee*i'Sding classu'branies fov dasignig usen ibtenJar
Java packoges ano dheufore classifted i'nto dwo types.
The ds Categoay is knouwn as usen dodined pactages amd
dho Se comd is knom as java APt packaqes
Java APT packaqes
Java APT p30Vedes a lanqe numben classes rouped
tmto diddount packages acordipg do dunchonalud
eg java lornq
Java to
3houdd
Java a wt edc
Finst leten of package
be small
The Jaua lomq packoge is a utorocticaly available
do al! proqroms od not
aqured to mpost Into dhe paogmn
expliady
CaRadtin9 package2
we mus dirsd doclovu dhe nme a dhe packa ge
using dho package keywoad dollowed by a packoqe name
ho we deJine a class
packoge tiasi package;
pubue class elassfirst
Heu dhe pack a9e nama is fiospackage. hr class
clascdirst ts mot Constded apadh dhu' packad
atu n oun ocw
packa9 Involves dhe dollo cwng
9deps
Declane dhe packoqe ad dhe
beginning e a dil as
gAep
lep1
dhe derm
package package nme;
9Hep Dedine dhe class Jhat ai do be put lo dhe package mcd dleclar
d publue
SHep 3 Cieate a sScebdirecdo umden dhe dui cdorg whe ve tho main
9
Scunce diles ane Sores
iep 9 to ed using as dhe class name java du io dhe subdui
cdey crated.
Heps Cempik dhe dile This ceates .class ile in dhe subdiantdrr
The Subdune
edora nam musd match Jh
[Link]
exacd
Accessinq A packaqe
AJten dhu packoge Sdateme n t , a yava dik may mdain
aset e import Sdatemsnt Impant sdatemsnts ar used to
biung centaun classesor endire into
package ou
prog*aro
Since we a u classes md midheds
ipordng we cdo net
nad o w Ate a tomoplet java program. The genenal fom
impot Sdole ment 's
Impert packogettpackage a- class. name/*
He
Hee packogetis Ohe name
ea top luvel package
Packaqe a is h e opdtonal inien packages, Sepanatud be a
[Link] a
speutc [Link] dhuth we want to fmpovt
Coi t spe cify ox a* CStan) cohich indu'tats dhat dhe
java
Compin shoudd use dhe endize packaqqe
Impet java util nate,
Impod Java to
Advantaqes o_usinq packades
They allow organi i n classes into unds
Theu &e diuce problenms widh Comt'cds in na mes
3 They allnw
prodecdingding class e md medhods ina langen
auys howo on a class b class bas1g
OSing A package
package pack1
pabuc vclass class A
p ublue Void olus play A )
s P C'class A)
3
Thus 8ocnce die Showd be named clats A Ja va
nd 9doved in dhe Subeluvcde7y Ciolclen) packt Vow
Compile dhs java dile . The Res wW dapt class A class cwill
be sdoved to dhe Same
Subduucdoy Now Coms icdun
Jhe codo belveo.
tm
pot packl-classA ;
elass packTes+
PSVM CS4ring ati)
class A objA = necw class AC);
oiA duploq A C);
Thus co de Shows a Stmm im
posts dhe c lass classA Jre
dhe paclkage packt The Sonce dilk
Ahouud be sawd as
packTestljava omd dhon
Compiled Now we coo sun the
p7oq rom omd obdain dhe aesul ts
Durung Jhe Comptlatian
packTest1 -.java Jhe
Conopilen checes do dhe dile class A class in dhe
pack
pack1
daridony tor iofovm ation Id needs. when dhe pack Test
is Aun fava loots tov dho dile packTest class md
proqram
loads it using elass loaden
V'stbiu'd Copd ollen
Access
Access Mocufrin pubuè |protecled7endyPivare, Pvate
locatbn Cdedaa t Pr oteced
Same class Yes Yes Yes Yes Yes
Subclas s in
Same package
Yes Yes Yes Yes Wo
cdhcn classes in
Yes Yes Yes No No
Sam pacEage
Subclass in Yes
odhen packoge Yes No
Ye No
on-Subclass
in odhn Yes No No No No
package3
he visibiidy modu dens ou also knotw as access
mo
d dieu Jaua provicu 5
dqpes visibilid modi'de
thy
They pro vicle olidieunt evels g protecdion.
pubuc
Anyvauèble oa mudhod ai vsible do dh entoa class
in cohith id is de dined d hes dhu wdut possible uerbilJ
oot a cres
sibiludy eveywhne
frendly
When no access moouiten is 6pei'jed dhe mumben
known as driendle lovel of access The $otendy aecess
makes dielda vsible omly n dhe same packg but not i
odhun [Link]
3
Protecded
omd tie
Ths til Ges bttween thu public
access
3 crus Tnad dhu prciecied modide makes r i dieid
giblnai cmy do md u b classes I Tra
all u ilasss
Som
**Gpact oqe but also t sub classs io odhur packgs
Tr keegn- Sub classes Iodh package Can no+ actus
Prird ed mediles
p7vat
a i occess
h dhu hahut dsgret f prctedio. dhy
mywIh dhun cwn class They connct be inhiuBed o m
dhauire nei accesible m Subclasstss
pTVab prctecied
Thus s a vsbiudy lae in betuween dhe pxtected
Ccess ornd pvat acces Ths mcdidier maku he delds
ISbls all subclause ugan dlass what package th
inTruse frvds not
au
occes bs by thu classu
In Th Sam packaqe
Ruls fev Applang Apprcpma Access Mediteis
C'SE publuc if ha dreld rs do be usible
eueyuohu
se pctected t dha deld is do-be usible
tutyuwhuu in
h Cursnt pockoge also 5ubclasses odhan
pacleanes
Use dideuwt Cdruendy) i dha deld
rs de be vsible
ovey whau in dhe cununt packaqe onle
tvat piciec #ed dhaield is do be
usible onu
1Subclasses,gandless e packoges.
Use private 1 dhs eld is ntd be vsible tuvey uwhau
zept in i6 cuwm cbss
Inierfoces Multiple (nheuda nce
jaua prourdis an altennat apprcach know
madple inhuu dorr
I74en dacu do suppt h i Concept e
of mere dhem ans
A Jaua chus cannoi be o Auh class
Bud id impkmint mcis Than cne
upe class co
inienace
Defning Tdenfore
An inten tace rs a krnd ct class Like clas ses, inten-
aces n d a i mu dheds omd varua bls but widh a majon
dhs d 1feumce dh at inHentace Conioins
odsdtuum cc 1s om
obsdaa ci me dhecds ond tina vauoblas. Thas is,
Cny
uhods 1n om iniajace j t em imple mented omd dhs
Vauoblu ajiga Cns dont valu a class iople mo nts
intenface dhat c lass must prevwcde dhe impls mundabon
fer the mdhed dodine in the mtenjace The ymda
Pe de drning om miata ce
1otenjace tmienjaceno me
pubuc absdzatt midhods
pubue imol vauoblls;
3
Im pumed1ng Intentaces
A closs dnat
Iopl ment om intendace has a
gene a
fe
class class name eziends supenrossno ma mpuments
otnfocy nome
IJ a a class tmp mends mORL dhom cne iden Jace
dhy o
bepanaird wrsh a
Comma ()1ha keq ccad
kcacad
mpome nts is used do intei ace md
mpument
widh dho class declanazon
.
utd in Covmjuncdion
Exfending Inteta ces
An Inlendace com be Sub iniedacerl f r o o t h
Hengaces The new sub inten dace will inherit a l gn
ms beS dhe sape intenlace
intenface aame tem const 3
nt Code -101
SHaing no me " Fav
Imten dace Ltem extenc
Itemo s t /
voied dusplay );
3
Im pomenHing Tntefaces-pT
ntendace A nea
final static float pt =
3-14
float oomputo C float z,floct q)3
class Recdorngle implomen Arua ?
floot Compute loat *,float y)
aeedom
udern C
**y); 3
class Circle implemenb Aua
?
float Compute Jloah * ,
floot
e dunn CPi * *A T); }.
elass Tntenface Test
PsVM CStaing ac)I
Recdomqle R neco Recdoole c;
CincleC new Cn cle (
S 0 P C"Anta q Recdoogle + +R-Compub Cto, 2);
S OP C "Aea ot circle = "+ C^ Coopute C0,0)
Caatnq An Asna
tnvolues 3 Sdeps
dsclaning dhe aOY
CA atung momaH Locaisans
3 puthing Valuns into dho memoy locathan
Declanung he anRaq
toms
tov dype anay nomeTI;
do a pe C] annauncum
C e atio o Avray
Coaung dhe aAoy using ned opnator as
aname = new qype [size
3 Tnidaluzaturò g anay
hs co be dlome using dhe avnay ubs cipds as
anay nameSobscaipt =valu
: a Ce =1o
we co also intdralize avvan Qtomatica ohen
ll dhey
Qe declane d as
dgpe aayname C) :bst of values
int aC]= o i 3 45 ene:s
S o adomotic alle
noy s1ze ts 6 nes
P o t o Soat a ust o nurobens
Vuable Si 2e aIAaYs
14 Java 4u ats mcldr odeimens ional anko as an
iays
avnays. 1d is possible do de claa a dwo cu'monsional
amy as sollocws
tnt oc C C) new int [3]C J
c [o) new intCD
C neco i'nt CA];
C] new int C3]:
These Sdateme nts C atesa dwo olimonslmal arnay
as having duidderent longdh fo7 each Aow as
c Co]Co]
P sont ust onumbeu.
class Soat
public sdatic Void Main sting aci)
in+ t J'
int a l) nuw int C);
a = 5 0 40 l0 30, 70, 100
int= a lengdh;
fo Ct' o; tkd; t++)
soP CaC]): .
Por C':o; tkl; t++)
Ca Ci'j< ats)
Intt a Ci)
a [ t ) a Co];
a Cul t
So PC Aften sondog
for Ct0, t'< c**)
s-0 P CaCu):.
i Consd Tucdo7 demo
class Beoc ?
doub & huaht
olo u ble coiddb;
double dpdt
BcxCdoabu h, dou ble w cdouble d)
24hus hight h:
dhi widdh =w
dhi dpdh =d;
votd volc)
S o P (volame oBox is"+ hught oiddh« depdh):
elass Boz Const
PS VM Csting ang C
Boc bt, ba
b = new Bo* Cio,a ,14
ba neo Boc a0 ,d, 24)
bi vol C);
volC33.
Consducdo ovenlo ad1ng dtmo
class Boc
doubl htght;
doubu widdh
dpubu elt pdh :
Boc Colouble h , doube co, doubG c
thus heqht = h;
Ohis. ewiddh w
dhui dt pdth = d;
Boz C)
dhus. hught ; -
dhs cotd dhA
dhus. depdh =6;
Vold volC)
S.0 P C "vol q boc is "+ heght * coiddh clupdh
N
33
class constoven /
PSUM Csdaing ang C)
Box bi, ba;
br no Boc (1a, 6)
w 14
ba ne Cw Boc C);
=
br vol o;
b vol c); t e o l i 24h
ghchthand Medhod
Stung Jaua
String class
Java hassding class dh at build into dhe java. lom
a
Packaqe The Sdoing class encapsulestov dhe dade sdTue
Sdruc d i
Sding ood pADvieles Sevena
Junechions builduing do
hod S6zi panupulatuo eas ien
Cons47uctovs o a Sting class
Tho Sd3ing elasSs has a set e ouenloa ded ans dru tdo
hat aMe used o cRe ate. Stings
341ing s ne S4ing C);
SAingC paamitu h u
i s dho da dawd tunsJrucdoy caidh no
is useddo Cretta o empdy Sdin
S4ing (s4ing valas)
Thu ConsdT ucdor is used do cteate a Sding olotd
pa3512g dhe SJ7ing Valu as angumonts duvcdy
Sdring chaa a t ) Stin S nsw string "Java)
3 Siing Chan a Cj
Thi Cons daurdo Jakes a chanacden annay as
angumo nts TIhi rs used do cRe ata a Sd 7ing dhat is
inidtal 2ed cwrdh dhe chanactens Stored in dhe ch anaten
annay
necw sizing C Chaa aC).
SdaingS
49d7ing Cchan aCi, int staat int Coupt):
Ths Coma dvuedo dakes dha chana cBen arnay Sdavdtting
integen emd courd as ang ument. 1isused do czeate a
9d7ing drvo dhe characten anay with a 8pecudied number
adin
chaact es ce Hrom a Sdantung chaacten
eg a C] "Java p«yra
Sd7ing S new Sd7ing (a, 31)
p apro.
5
St1ingCbyte a»ctiC], byte huqhbyte);
Ths isused do cacate a
Seaing drom a byke a a Tn
dhu cwe have do pass dhe
byfe aay Opd huqh bqe o
Pach chanacden as
().
agume nts
byte aseii 65,66, 67
ta)
34ing S nna
S4»ing asc,o) Cp A BC
yte asciit), byte hahbye
ind Sdat tnt
Count
s Conuducdor accepds a byle aay hghbyt
cates
Sd ant i dec omd Count as anqumens. CR a
dro dhe ble asnoy dror a Apeed Stanting
Sdring
Indac amd having dhe numben ch coractens in dhe
Count
eq: s+*
yte a- i65,s6,67, 8,69, 70,7
8ding S new Sding (a ,o,a, 3)D;
O/p cOE.
Sting Medhels
SdTing elasc eodines a number o modhocls Ohat
allo w us do accoopu'sh a Vanreky Siing meniu atin
dask
Medhod call Task Pendomed .
sa S1toLowenlCose ; Covets dhe staing SI do al
owen case
Sa =
S do Uppe Case, Con vertsdhe. sd 7ing St do all
CPpen Case
Sa =31 9place C J) | Replace al
aPpeanances o
co idh
s St 4im C); Remove3 Cwhude spaces at dhe
beagnning omd end dhe St1
SI.
St eqals Csa) Redunns drue id Si is
e9ual do S3
St equals lgnoseCase (Sa) Redrns dnue fSI equal to Sa
anong dhe Ccue q chanacten
S ongdhC); Redunns dhe
legdh J S1
St cha At (n) Guives dhe n°h
chanaden s i
9 Comparelo Csa)D; Redans ve t Si Sa , +ve
S S a m d o f S1 Sa
Concatanates S omd Sa
SI Concat ( Sa) ; dh
S Substing Cn) Cives Sub sd7inq SJanting tromn
chanacBen
Om
S Subsd7iog Co,m); Gives Bubsdzing Sdantin9
h (nott
nO chanacten upko
includuina mdh)
Aile a
pqo betical
fo alphabe ondening sAings ou tan
ings
give
'Ve dhe sting explicidlg