Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

INDEX

. 0
TOPIC 1 AJE PAGE NO. SI G .
I Company Profile 7

2 lmroduction To Ja\'a 8

3 The Ja\'a l'lalfonn 9

4 lnstalL11ion Of Ja\'a 10

s Configuring Variable 11

6 \Vrilc.Compile And Run aJa\'a Program 13

7 Package 15

8 Cbss And Objec1 16

9 lnheritnncc 17
10 Variable And Method 18

II Modifiers And lmpon Sta1emen1 20

12 Interface 22

13 \Volking \Vith Cbsscs 24

14 IDE 27

15 History of Java 40

16 Fcaturcs ofJava 43

0132csl 11116 I
Introduction to Java

Wih d1C nwrton of nlieftlf>rootS."'11>. 111" \\'Or+.! " >C1C1 mly dc"'bped "ill .op!U,1.,,,1ed
C<l1J4'111Cr<S. sysiem;, ond dc\iccs. Maoprocessors arc l6Cd 11 COfl'l>lltt<. ielevisons. aid
lilX n<1<1w1C., f\'Cn lhc hlnd·held dc\1e"' ..ch a, P"l"O.. PDA> (P ml DigJlal As;.1>tn1•
ord eel phones nnc use of rric""'""1CSSOl'S. Al lhese clocll'Onl< dC\'ilcs are he"ful because of
l.h.!r eo1nTU11Catl()n capabific:-.. \Vllh 1h.! •'CTC"asilw cupabilil onJ dai.... c0$1I or
11bnro1ion PIOCCSS"1! and 11C<\\'Or1<"11 1ecmobgies. lhc ne1wo11' Ii grow1111
rapid!) !Or
lm®nlllina idomuiion 1hroui;l1 tl<c1ronie S)'il<""-

htcmel • 111" ""'"oil< o( 11"1\\uib b«wttn ddkrenl l)l'<S of coirpAlel') localal al


d1fl'f.'Cl1l
al pbces
a chcaptr ruie IOh•cmcl
ti lhc 1ransmi nmoon.
Th1>. lnbmniion
lhc lnien1e1 ha> can
nndereach IO any
lhc wot\l pbcc in\ilbi;t
a a;bb31 br quick!)
lhe "'Ott!
11bmn1ic>n •><h:lr@e. n.. tn llli'nsln.ca.rc of tlecuonic dcvt-.es •nd ncrcomecied
COn1""<r notwod.> crcBlc an "'1\TO.....,.• dul l""'CnlS """ ch.1 IO .olw.u.-
ilr lM e11e1g1g CC>nl'tdlt; cmT01m:rt. Ja\'3 process lO be a \\'Cl iidl1>U'ic<.
bre."9'· • is build suaable "' ne1,.od.ed ""'"""''"',.. i"uil'l'll! a iroat ""n..-tYsUICd
of <O"l'utcr
prograrmq
•nd dc\iccs.

,. rturonn ind<ptndtntt • Many i:uvnll'S are OO"l"'•l>le "'ih oriy one pl:ui>nn Ja'" Wlb
"11CCdicoll) d<>.,..,.:I so 1h11 • \\'Oiild run on any conlltlcr. rcprdl..-S> 1r • was nnwng
Wildo""·LnL-. Mac. Unix orany of the other open lclllii.
, Sln.,tr and t•>)' 10 ust .. J:&\7t'S crcal 1R.".<1 10 dcsigJ1 IM> code couU be \\T'lllcn
effic:1011ly nrd ca<il).
,. M ultl fllDCllonal • Ja\'a can produce m'II) n1>plic•t1i<>1" from co1motd·l'1c program• to
tipplels tO Swi1! windo\\'S (lmsa:aly. sopliiitca1ed graphical user ).

'""' does l<we son"' dr.1., ks. Sitt I hos •l.IOlmtcd i;uboge rol'.CllOfl. I can IO
n'Orc "'""''Y dnn Olher 8C$- There arc often 111lk'.1n'l.'lllt1011 ddi'.l'CllCC$
use
difl'.1C111 pb1i>rm<. "hic!i "'" led 10'"'"
unibr on btirl! de<ert>cd as a '\\'rile once. IC'I C\'Cl')"'tierc•
S)"lcrn. l..Mlly. U1CC l """ an ob,1ract '\'W1WI n'Dl:hir". a i;tr"'"" Ja\:1 P"'ll"""' 00.,,.n "'"" ocecss
to lhc l'atr.'< APl's on a tern dircc1ly. 1'onc of these i;suos arc mml but I can 111ean
th:tt J:1\'3 tin•t an ap1>ropri:•tc chocc.- IOr a utic:ubr pteee o( !!o<>h'3.

0132csll 1116 I
The Java Platform
One thina ""' distioJishcd JB\11 from som: other bro<t is as abilily to rm 1hc "'""'
ca1rpilcd code across "" le opcllllr@ · .In odn t•l!C>·thc source code (oo<k: 11101 is
wmen by 1he prol!"'n'll'CI'). is corrpilccl by a con"llilor inlO an c"<Ccwblc file. This file is in
m:.cbn: t111g11:•ll"·and is ncndcd IOr a sq;Jc opcrn•il! S) tcm'procl'SSOr co.mimw1\ so
d'C pmlll""mll.T would tu": IO re corrpilc the progrnm s<pcm icly fcir Cllch '"" opcmlif.:
sy.lcm proocs.or co.mna1ion.Ja•11 is dd!Cre,. in tha1 • docs not con..,ilc the code drccdy i110
m:ichn: Dfll:ltll): code. Co1rvl11im creates b)1t-.codc oll of lhc liOU!tC code.
13)1'Xodc lli'n.:rally boks SOn'Cthmg tile thti:

a7 73 Sa lb 92 7d
When ""' code is l\lrl by the user. il ., proc<:ssed by SOt1'Cthil!
coled the Jr"" Vinull Mxhile
(JVM). Tho J1is cs<.emolly on inlcrpre1cr IOr the bytecode h
llJlC< •"""'111•1he b)1eoo<k: nnd runs a. There arc dili:re1u
versions of lhc J VM d<1t are con..,ati>lc "'th each OS and <:4n rm
""' smn:code. 11...,., is \Ttl<lly no difli!rcncc br the end-user. b111
th5 nukes ii 11 lot easier li>r
PN1Pn1n..... .s doing soft\\'firt dC\'Ck> J>11\:'nl.

Java an d Open Sou rce:-


, In 2006 Sun stoned to nuke J11va a•11ibblc l.llder the GN U
Gcnet1ll Plbli: Liocnsc (GPL). Orncle co111inrs ti... proJCCI
calt-.d OJ11!1t.1DK.

Java Virtual machine :-


,Tho Java \Ttml rmchine (J 1) is a software irplerne.-ation
of a COtrpllcr dm
o-cute> prosrorm lilc n real 1mchrre.
, Tho J0\"3 \Ttl<ll nnch111e is "Tu011 spccrfic:llly br n spccifi.:
opcr.uing sysiem. e.g. !Or Lr.... n <peeill it"1>len'Cn1:11ion rs
n-.quiOO a.woll as br Windows.
, Ja,,. P'°1!13"' are corrpiled by the J•v• corrpiler iuo hyrrrode.
The Ja " vnwl rmchine
i1•<-rpre1> this hy1< '0d•nnd execurcs the Ja,.. p'°""n'

Java Rum time Environment vs. Ja\la


Development Kit
,,, A Ja\'D d"°trbi.ik>n conl!S 1ypi:aly inl\\ro fb\'C)l'S. ttw: Ja1•a
0132csl 11116 l Rl111timt E11,ironme111 (JRE)
and the J1111·1 Dr1·e/Of1111en1Kit (JDK).
, Tho JO\"> rurnm! em-i-o1""""' (JRE) cor\5islS or the JVM and 1he
I nstallation of J ava

•• Check installation

, Ja'" •¥ a"'*ly be n.111'CI. on >"" 1111<hno You can icq lho. by Clpl!
l•C 1console
c.r you .... usre Wnlo\\ : 'n+R. ...._.,. cnu and press Ener)
,If •ndn.1.al.'<l.
Ja\11 " com'.Clly ))' '>1"'1:)W
., lhe
'11c>uld 'ICC .Wnnr.ll)ll ...,.. )Ola' Ja\ll
n<tl buon. If lhe'"Icommndoonm•ndinc rellrn< lhe ilbmtuon dut lhe protpm coulJ mt be
,P,\2 •\'ttilOO
'-J.)\lU'""' IO n.iall J.l\11.

Installing the Java Developn1en t K it


, llciln: lll§t.1qlhe Ja'" Dc'clopn1:111 Kl (JDKJ. )VU .tlO<llJ probobl) ll10\\ """ I &.
h • dbriM.lc:d by Omclc h CO<Uni the core lbranos at>I conilY. reqUl'Cd IO
dc\'Clop
J0\1Th: JDK shoot! mt be conh<cd "-ill lh< JRE (J""
Runim;: l:Jr.'l"OllDr,. 1'hc
JR£ ti a IVM i>r nmona. tiorroscd 10COO'lliing. l»ll propu1n..

Do"•nloading and Installin g


, ro do\\llb:ld die JOK. IP
IO
"lh
t!tn. ""'" OOlj\; romtcsln.1\\00
ctBcom llla'ldlc . folbw
@'1lhe ''"'nbmb&Ir
i•pw1\1111.ct!On\ do"nbadrc
!!k:\hllri . lhe JOK
ClillN>lluon

"'°"
fil on
e\VindO\\S: If)W are ....... IYW!do\\>. \lrc>I)' nm lh< e>c<:wbl: file at>I •JDK lhe
nst.lll1M n>in.oon.
Unix, Solaris. or Unux: tor I.no< and UM. downblcl die 'ldl-1 6.0- &Ir ln1t
' " Sa,.c die downk•kcl lilc ., any dmc.Once )W """s;i\"C:d die lie. c'111ICI I IO
a P*" lh.11 )W CUI ranmbcr. by u;q; Tcrmn:il or b) clolble clr:lrc on die lie.
Wh-.n )'OU ID\'C li-..hcd CWll<"1! lh< lilt.copy die JDK I60 i>U<r anl Jl"Sli: I ., lhc
wloca•Jo paS1C IO die wbcal dRCIOI)'.)W"' 'IO be ".-) so ""' C\a)" ll!Cf can
""' lhe JI'" h You •.,.dd:.tc lh< do'A1*>1tk'CI fil: \0 11'11 I doan"I !ale >I"""'
on )Ola' dmc.
la cf ntosh: The: b1oi G\llllble JDK " a..01miol)' l\lia.. ..i die opcn11rc
by
•)"Siem llccousc J.,,.
i>r l\bcirn.h " dl:'Cbpcd and nmunod by Apple. n
coonlnuion "lh SIA die Nl'CrC 'mon on lhc \11enosl1 RI>) not be die ,.
,..,... ,.""' ""'"ibblc

ote on Editions
, 1'hcJDKc:on>OSn
drcccd1<>11L

OIJ?csl 11116 I
,. Ja•-.Slllndnrd l:dllon (JSE) nus \'CrSIOO " lhe base pblilm1 i>r Jn\'a. The course
wl k on t:d•1rJ1\.

,. Ja•-a
d1>1ri>Ulcd nd•o;.>r
l:.nlctpl'l>C (JI:.!!)
l:.d•on 1rch11«Hrc Tho.
Juv.acdom " 1ro11ly
•ppbltJ<b. b;i '"bq,\cly on and """
dc\1:"""'1g
co1T110nct1S Nri1G on on apploc:uion .mer.We wiD no1 be
°""'"'11: rro .ot"" •m.IOn 11
COll'>C,
,. J.,., Mrro Ed•on (JME) Tho. cdtlOll " p"1mily i>r d!:''Ck>pl\J d.., "'
Pl'Ol:l'l'"'
1hc
10 run on
CO!Nll\."r applt.:aroce>. ,11;h 11'PDA> and cd
phone< . .
Configu ring Va riables
,. llcM .,,.,. .code. • "' reco1111\....Jcd d<lt )<Kl Sci 11., l'illh •-ari;1ble on )<lur •>•cm
so)<Kl can con'!Jile yow code n'IOrc easily.

•:• For lflifldows Users

,. from lhc Co1irolPoncl do<ble clo;:L "S)'icm" (Sy..1cm and Maiurooec 11 V1>111)
,. For Window< 7 or Vi.tu. click on "S) lcn\• "Ad'1111Ccd S) leln Sc11• on Ille let. and
11""1 on"l:rnironnli!n l Vlll'Ublcs.•
,. For XP and 2000.ck on lhe "Ad•-.nccd" lllb and ck on ilm'l'\lrn>\.'l"t Varobl...- " for
NT.clr.:k on lhc •mironmc1•" iab.
,. Scl'CI 1hc Pulh \"Jmblc ond dkL i:.d•"
,. Adel ie p:uh IO ie b11 drcclO<)' of "lli!rc Ja\11 1> lSlal'd on )our hard dl1\"- II
probably be C"'ro•m f'bVu•-a )<IL I 6.0 20.bin
shoukl 11llcs$ )<Kl cl\s'll duri1J

in>talb1ion.
,. Ck:L OK
•:• For lifllL't: afld UNIX
,. Ore "'flY IO >Ct )<lllr pa1h 11 Lnllt!Un,. !;IO add a p.ilh cxpon w yow bash profile.
,. In order IO do lhl>. In! open yow b:t>h profile in a ICXI cdor For c'<lln'!Jlc.
,. pko -1.baiJl_profile
,. ,,,..,. udd lhi< line:
,. e>.pe>n PA111 SPATI!iusr locol Jdklbm
,. Note dot dc palh IO 11-c ju\-a dio'CCl<liy •l"r'\icll ,.ik>n" 1my be ddlc
on )'Our
nnchl C.
,. Rc<iart your •l-cl

0132csll I 116 j
•:• For Muci11tosh
;. Apple sets c•ihing 14) br you. Si ba.ck ard rebx.
,. "fbo only dnl\\back ti tlklt bl'C3U.C j\ppl: ltuidb dC\\!kJt)nr1• und nnl1le1'111CC
JJl\'a !here ti usualy n dcby tom lhc 1ire tl'<lt n n-.w'""'"" ;,, relcose<I by Sun
of M3c,
on lhc
111.t the tin.:th:it the new '""' ii rcl!ase<l on dlC Moc. Also. l)'lli1g dic btcst \
som:to1...s "'<l""''S an op'.r.lliJG sy>1cm
cnion up!
lr'ldc. °"
,. Oh \W ll )\lll can't "'"c\\!ry1lung.
Va lidate installation
, S\\"lch a,pil 10 the co1l1tnnd line :u n11 lhe tOl>\\'ing co1muncL
,. ja'ta ..,\:!Oil
', The ou1pu1 should be sin br to lhc blowing m•pUL
,. ja,..'""'"'" "I.7.0_2S"
,. OpcnJDK R'""''"' EnvironO'lcnt (lccdTeo 2.3.10)(7u2S-2.3.IO- I ublln .13.04.2
,. OpcnJDK 64-81Scn<er VM (bu&! 2J.7-b01. nix.:d n'Odc) )

Dcvelopn1cn t Process \Vi th Java


,. JD\':J SO\rCC fiL..,;; nre \\l'iten 11$ pt1i'I lCXI dOC\lll.'111.'i. The prowa1111l'..T 1)1Ji.;:i11y \\
Tiles
Ja\'3 M>WCC code in an /111egratt.c-d De-.·clopn1e111 E11viro,,111e111 (IDE) klr pron•t-q.
Ao IDF >1ippon> lhc pro11rmn11:r in 111: ta<k orwrit1111 code. c.ai. ipmvik< lltJIO·
IOnn.11i111 orlhc soin:c code.,.illight iny or lhc in.,011:1111 keywords.""'·
,. At <011a: poii dlC pmpun11>:r (or the IDE) c•lls the Jn,a convitcr (jlmtc). 'Ibo J1m1
CO"l'ilcr creatcS tl1: byt<-code 11Struetions. 111:S<: D1Sll\JCICllS an: Stored il .clau 61;,,
111.tc1111be CM'.Cut<il bytt-c Ja,11 Vin111I Mochia:.

Garbage collector
,. The JVM mmormti.'.aly n: col.".C<S lhe m::mory \\loch " not n:lcrred 10 by other obje<ts.
'Ille jaw g11rl>uge roll< ·tor check• al object rel'n:nccs and Md tt-c objects \\hich can be
11U1<> 1mlL'lllly rel.!a>cd.
,. While 1hc tba-c collector rel-a<es tho: prosromm:r li'om lhe rlCCd 10 cxplicaly
nnnajje n>.'.lllJI)' dli! progr>1T111i!r still need lO ensure th:il he d0<-s not <'<P
1111-..,.,c'<l objccl rel'n:.-.'.C'S otherwise the i;:1tbai;c col>. .:tor C011110t relc:be the
associaled 111Cn10ty. K"'Pi11! lllllCCdcd obJCCl rctcrenccs arc lypacnlly cl>'.<l memo"'
leak;.

0132csl11116 I
Classpath
,. 11'c clt1t</><llh dcfin;:.. whore th!Ja•-a con-..il<rond JO\-. n1ii110 look !Or •o
fil.'< to
looo.'111t. ln<ll\ICtlon can be t-i 11 d"C Jn"• progninl
,. For cxa"1'1c { you \\'ll!U to '"" An «tcm>I J11 2 lihllll)' )UU hil\'c to add tho< lhr.vy to
)'OW' cll>spath to ""' 101)Uur prol!J'llm

Wri te, compile and run a Java progra111


:·Write sou rce code
,. The lillbwi111 Jt1\'ll pro'1\\m i. dc,.,lop<.-d under l.nJ. "'Iii
a tc"1 cdior and t
l'C co1nm11d ll"C. 11'1: procCS5 on odlCr opcnitoljl S)'>tcm >ho'*l be sonibr ard
bta " not co'""'d n th;. dc<e'1'tklll
dC'\Clll>n
,. Select ll new n(
II pnth
or create vt """'"
d"'ctOI)' l• .tv•
wilt be
o1 mc<l le ui.cd
r t• &>r )'OW' On lcro)()ft \Vftl\1\\')
Ja>2 dc,.,lo1in10r1. In t)'Olr
hti
1¥ "':lr1 to lbC :\t.U'lp\J•Y••tor ter. 'lbb path is cab! j u,·t1ti1' i1 the 10l1\Vilg
dcsc11111on. Open 1 text cdior \\lllch • 4'pons pl1in text. c.a. II"'" wldct Lirll.'< or
NOi ""' inlcr Wndows and d1C &>llow • code.

HelloW01Id x
II A Sftall Jav1 progr.,. publlc cl••
lloworld (
publtc 1t1ttc vold ftaln(Strlng[)args)(
Systeft .out.prlntln'(twllo Wor
ld.);
}

c• TabWldtl L.116,Col
5.,.
INS
8• Z
1hc '°urec code i1 )Ullr ja,·atllr din'.CIOry \\..h the H• l w d.1•v• &:n,u .Tiic nan-r
or• J"'"so file nut ah'"'Y" e<11.11h rhc ck1ss n:1n"C (withn 1b: "°'"'° t-.odc) nnd end,
\Y1.h the .ja • C'\lC'lbion. In this C\lln1)1c lhc fir1u1 n•bl be tt lo" r l<i. av•
11'u. usc d'C cb's ;, call::d 11 •or ..

•• Com pile an d run your Java progra n1

0132csl 11116 l
,. 01>tn u she ll for oornmand line ICetSS. S"'llCh 10 the }t11 adir dn".CtOI)'
1>ih'<ltnmnd
the cd j11•..dir. IOr ""'"'111<
i1 tho obo•...- c'1l1111lc
vii t he e
conmmd. Use the 1 co111mrd (dlr under M i.-.rosoft
\ h "'•\• qella\ J•• •t•rtor
\Vtndow,) lO \'Crify lhJ1 the .om:c filo b 11 1hc diro<iory.Con.,il: ymr Jo•11 .ourcc
Iii!
u-.o acbss lilc wilh the li>lklwing comnund.

djava c HeDo\Vorld.ja va
conmino now o &le 1lclb\Vo1'kl.cbss". I f )\Ju ...c tho; file you
rccwry fully COfl'1il-d
SUC«S<. h:m: yutr fl'SI Jll\11 SOll'CC code into
t\ffcrwnrds !tit uin lhe eo1•cn1 of tile dn'<'IOI)' wilt t he II or dlr cormnrd 11v:
b)1ccOOO.
You con OO\\' stan )1011r 00111lib:I Jtt\71 !Ptm F.nstR 1h:lt you 11re stl i1 the •rdir
di'CC!ory ond mer the folbwing oonm111d to >1un yotr J11•11 proi;rani

j!lV!l Hcl!oWorld

·me system should "nc '11cllo


Wortt• on the commnd line.

Using the classpath


,. You cun use the classpath 10 ""dv: progrom &om another pbcc in )UUI' d""'"'°'Y'·
,. Swach to 1lv: con'1ll1•I Inc, e.g. under Wi1dow> Stin·> R111 > c1rd. Sw-.:11 oo
"'1Y
dircc1ory )Ull ""'' ·l)pc:

java HclloWorld
If yuu ore oot in the di'CCtory i1 "ilich the compil..-d class is <torcd tlv:n the S) cm
jon.li1n;:.N
rc oCbu
ult)'J'COtfFnund Error: rtsllff
shoukl
,. To usc the class
utCl•ss "nmi11 11
the Ml cm>r
i>lbwa1g 1ocss.:1.,:
con1m1•I. RcpbccExcept.ion In
"mydi'cclOry" "•h the
lhreod

dr"."Ctory
wheh corinils the lcsl dR.-clory. You <houkl oi;iin $'"lhc ,clloWorU " 01nplA.
0132cs 111116

l
ja,•a -classpat h "mydirectory" H ello\Vorid

Java basic tcr n1s


•• Ba sics: Package, Cra ss and Object
h Iifl1IOIWll to onkNard lhc base l<"t'lllllOlo&Y ofJa•1111 l<lm>
or/ldt4ug<«.c ..,... anJ
oh/tr:«.1'hb scctm l!J'"CS anO\\'ni<W ofth.'SC tam..

•• Package

P11eon:typally u.cd to lPl'.ll4' cbs>a no bp:al -.. ror•.......,11: al


·" fP •t w
' pplrical
II"rormnn practlCC to""'lhc ...-"™'
, , oronappir:auon n1lltil be pbcc<l
clot1ll11 mmo or11the .,.,.,package cal«!
dieCOf11111flY astop "'"'
JW1'lll".
pack.,.. of lha. ny SWb wch
For<W11'1i: die con..,.,,,. n'llta own die donun •'Ol."lt corn :and 11 lhli c...u'1'1i:
d..Ja"'
()th.or rmil tell i>r lhc INl'1< of packlF' "to a'od colJi,on> of cll'-<. a. /\
col.on ocan rf lwo rom:nam to a cb». n..Juli)•
rm\T. S 1:1'" lhc ..,,., fulh• qualifiNI
q11al1("'111umora cb"11 J"11 con;i-.1> ooi of die pa<ka;r: mm: blb"cd by a clot (.) and
lhc ells> nwrc.

\\ thu pack•.,,..•J>R¥1ll11'1'\I.' rroy CTCOIC •"" cbs> coled "Anodrr propu1rm:r


""Y Ctelllc a cb'' "•hdie ,,;im: ""'"'·\\ ch lhc INl'1< of pac:lage> )OU can 1d
"11>.:h cb» tocal for ""'""le rf lhc firu prowarrmer .,.a.die .• • clh> no pac:k•se
')'ltll'
lhc
ond the .ccorrJ J"O....,..,...,. )ltJIS hi< cW.. • )OU can
dt.tilgubh bdwccn lhcsc classd b) U>irl; die Juli!' quuli(U'd 11u•c.e.g. • r... '.1 or
no P"'l.allC
• •

•• Class
O.f..T<111platr 1hu1<i<'l<nbtl tlrt• d<1tuunJIH-lr<l•·ior<mtx1<11t•l >111/r un
1mtun<efl{t/1J /
cloi

In JO\'I <Cl\ICC code •class "clcliicd by lhc f1<1n keyword and nu.I>tan "rrh 11 capal
bicr. 11ic body ora ells> "swrounlcd by 11.

O IJ2csl II ll6 (
package- te-3t;

lb:dalll &>SOCistcd wih achs.s i.stored in •·ariuhh:s :die lxhol'or IS>()Cillcd toa elm
or object is in1ikm:nu:d wib mnhods.

A cbss is coccmcd ina Jo"' scucc Ii: wib Ur s:mr mm: as th: cbss pbs th:.j:l\11
cmn<lm.

·:• Object
Def.:A11 object a w1 u1stantt of a cl=

1b:objeCI is the real e,klrera wbrh lus <bu ord can pai>mi a<:llons. Each obp:1 as
ere:iu:d based on the cbssdcliniion.

0132csl 11116 I
•· I nhcritance
ACDS> canbedctn'Cd &omanotherelm. In Lhil case1his cbss i< cab!
•sr1h"1LUS.Ano1hcr
C0111'1lJf1 l>tnw b tJ\at Dt/t1U t'Clt•nd\'llltOlh<•r tlil.'U

lrllcru ncc alb" •ems 10 '"""" d1C bcttl\>Jr and dnia dclhlKll.. of ml>lhcr cb>s.

!he (>Jowrna codes dcnll...n1c, how a d.ss cnn t\ltl' another clN. In Jl\;i 1CDS> con
c"cnd 11m><ir11um orone cllss.
p ka c .vc
claaa Mya.1.Cl11a t
void l'l•llof)f
Sytte&.ot.ptlntln c •aello fro.M)'9e1CC:l•
•> ;

.;, Overridemelhodsan d lhe@'override 11nno1111ion

rr. cb_q " "'""' another cl:ID • rilr:nt< lhr: rrcdlll<h fi'om b "41<'r<llss. " \\'lll'b to ch.'111S"
1hcsc n>:lhods 1can ""emJ1hcsc n>:dlods.To O\=dc arn'.lhod )OU ""'thesan>: n>:thod
•Y.1un: 11 1hc .oun:c code of 1hc >1ibcliL"-

To 1'1d.:a1c llO II>: n:odcr oflhc source codetudlhr: Ja,11oon.,ik-.r 1tr:11 ,au hi' lhr:
soO\'l"T1"1ie 1rrethod you canU§C the •o r
n1cr1oon
nnnot1110
r n.
Th win& codedcrmn;lrole< how>"" can O\=dC •n>:ll'<ld fitlm • bss.
e
·ka c .vo.,. la. ava 'lt r .ba

cl••Hf811eCl•aa I
void hel lo( l I
ly1to•.out.pr l•tlnc 11e110 1e<:l11e
t1oe H 11

c.1.aaa My&atena ionClaaa2 • t•nd•


Mya.a.Claaa

Object has superclass

0132csl 11116 l
E'W)' WJ '1J'1!irrf>h;i!ly xr<;n,jj !lie t•1 .Th: t <!m\!!;- iP.J
m.>.thods r <>'Cf}' J3\'0I ;
objccL
,. •'I•• 1 u all"' iochcc (lhc Cll'rell object ti cqwl 10 01

,. • 'ch ti n:1m» d1Ccbss of ch: object


,twshCodf!- 4 , returns an ntifJCr of lhc cwrcrt objoo
,. <
ti G•'C:1seqrq>ttSCIUIJOn ofthoCUTl!'ll object
•tr
Variables and methods
•• ariable
Variables al>w llrJa'"program IDs1ore "'klcs durq llr rullUre oflhc progr:un
A ,"Jl'Db£ can cilrr be :tprilltiti\ ''anab/e ora "/ 11ce ''l1riable.A
plinli\"C \"3.ri:tbE colllan •'llbc \\IW tho n:5:rmcc •">tilbl< coiun 1n:l'mXc (podi:r) 10 !
he obJ<CL Hence if you COf11l'll'C two """"'""' \'1ritbl<s. )'JU COIJ1l3fC ifboth pol1l IO the
sam: objecL To C0111'3n: object> l1'C !he ...,, c<.·"""" 1100J•.:•21 mcd>Od cal

•• lnstance variable
lnslnnec wroblc " ossoculcd \\ith on insuncc uf tlr cbss (al.o calbl obj«'I).Acc<'SS "'Orks0\'Cf
tlrscobjects.
lnsl!l""' •wb-s can ht\'c any oett<S eo11rol and con be rmrtcd hi or cran;>:fl. c-
•wbl<s rrwiccd as fmJ can nol bec""""' alrr lb5\!11cd IO a ,,,u.,.
•• Local variable

jmalt> lieo11\ld1ft'.I' nv:tl!blc 10kle:ll 1'iltllblcs. Thi; troddier defines cm!lielJftlb'


lc connot be chi'<! ofter&i.a L'-'\!Jlm:rc.

Loco!•'Oriobl<> donot Ftdcflul "'b:"so !hey 1111>1 benaia11.lCd ben:"'"

•!• lethods

ou2cs111 1 16 I
A n'IOlhod t> 1block orcodewth p>r.tnltterS Ol1d n rrtlnl '"b:. II can becalled on
the
objcc
publ ic cla11 HtbodExawpl• C
void t•1t er (C1 r l n9 II f
Syat••.out.println(" ello Wo11c J:

Mdhod can be dc.;blt(J w•h ,..r-ari;..In tl1'l ca>C lhc n'IOdlOd dccbrcs 1
\\heh •CttJ•L' &um l'Cl\l IOirony ,...,_, ., (s)TCll.:l)f>C m1l'IO.) A n'IOlhod c•n
paran'IOIL"I'
"'""one "'"·atg; i>ftll'IOICr und lhl'! """'be 1hcbs1pororn:t:r
only nlhc ll'IOdlOd.

O'"""nc ofa S'4'C"'bSS n-cdlOd:/\ n'IOdlOd n'Uil be of d'IC CQC1 S31l'IO rcllnl
po.......,tcr "nd the'""" a...,an.'IU.Al.a lhc rctlnl p;1111n'IOl<:f nu.t be lhc """"'·
Q,.,Qa<J n>'.lhod< An U\'\'.fluaJcJ nx:Uud eo. • n"dW "ch lhc wn-w: .-la:. hue
d&f\! "'P-ll:•"b.. 11lC ""'"" l)'PC c11nnot be U>CJ 1(1O\'Ctiolld o n>"."thod

•• l\tain n1ct hod


smti: n\:lhod "th tl-c &>llo"
A p<iili: '"»
•v>tlt.n: can be ......i 10smn 1J&\'ll
11ppli:ation. Suell o m:thod "l)'Pc•lly called • n"1hod.
•Ol 1l •l c

•• Constructor
" CbSS CIOIU" ron,,,,,,,Or\ 1h:U arc 11'\0J..t". d to C:tt31C ObJCCU on It.: CbSS
dchlon. ColllUUCtor dccbmto"' bol Ile n'IOthod dccbm101" c"'cpt d'll lhcy use tl-c
mn-c of lhc cbss nl ta"'
oo rc11ni l)'PC A cb<s can """ SC\"1111 co1\'iuu::1or11 wih
ddi"l'C111 parall'IOlc Loch cl1ss mJ.\l dcf11C DI b>Ione «ll"1ruCIOr
Inlhc win& C.\Jlll1)lo lhc con.ll\ICIOr o(d'IC class C'P""lS
k• •
poran-clcr

... 1 •

•••1
•v• t' .

St rifMI II

public H,Con1tructortxaq>le21St r1n9 al C t hla.a •


I
I

0132csl II116 I
lfno•'Plioll oonslNCtOr "defined lh:COITf!il'.r odds"'l'hi:1tly •oonstn.c10r 1rin:
ells>
.. .....< thon lh: conitt\ICIOf of lh: ...,..cbss " M";I)'"'l'IC1t ly cabl II ti-..
ca>c.
In lh: blowm11 <>Wrc>I:: lh: dclinton or lh: c:ori<tructor ,..,.,.. p;llll rc1m. (:al.o

l11D"n astl1e twr/111 "'""""tori"llWICCCSSll)I. Ifnot <p«ilicd lh: corrc>kr •o.IU


CfQlC ont4

public
Cl•••M,Con1trctor£x ....,1•
•I •public
ti
M,ConatructorEx&lfPl•t J

' I

t
11\!mltWlg """",..."" brcrcatq acoo"rna:tor "the b""'mg. , .......
..
obp:t •cn-aled ba>Cd ona con;cructor Thi. CCftMlC10r nWlod •lh:
•iatm..,. cal..S bcbrc al1)"1ing elsecan bedone....... the
obJCCI.

Modifiers
.:. Access n1odi liers
11\!rc arc nl>Cla.,.. kc)'words 1\2ilbl:: 11 J0\2 />UNu-. I'"""
"'d and
dYCC an: w accc<s l::wl..
11\!rc pubt.c. protoctnl. J<fo11/1 and pn••ot. l1IC) dc&nc hov. lh:
I'"'or.
c:omspoud..cl::m'.111 a '"l>I:: to other C:Ol1"f!Oncrti. If <01R'.111111 " dcclll'<'d
cb.s.cs or n'Clho<h can be ice!> crca...S or callod by other
publi:.c.a. Ja,,.
objCCU. If son>::dq 11
dccbl'<'d pn12tc, •-II- • n.'lhod. • can only be aa:cssal "-chll the class 11 ..111cfl • "
dcdmd. pn>1oc1n/ llnd dtfuu/1 ore snmr. A pn>trc1"'1 ells> can be ICCCSSCd li'om lh:
pac:k•ac ol\l'lb-cb.<>a o....X the p.'IClaWo' "'*• tl<fuu/1 clL-s can 'Ionly

aa:c<.<cd ..
tllC wn: pac:l.a 11\! blo"111g tllbl:: dcscrl>cs tllC ,..,l>1bty:

Table I.Access Level

\lodllltr °"" r..ck•i:• Subrlan \\'oriel


N>lc ,' Y Y Y
protcct<d Y Y Y N

01320$111116 l
no y
imdifa N
N N
y
Pnl'OIC

Y
Oth er niodific rs
,hi m:thod> <•mot beO\TClcn 11•abcb»
, abitrllet ircthod: no n-.:1hod body
,. i)11tll'onlll'.d n>:lhod '. lhm11 "'"·can be fml and """ any OCCCS< cor1rol
,. mll\'C rn'.tl-ods: pbti>nn clcpcroa• code.1pply only 10n>:lhods
,"1n:I ebS> Ot' 11\:1hod

ln1por t statcn1cnts
•:• Usagt of import statemen ts
In Jna )OU I"'"" 10 occc.sach» •"-> ,..u W.q.. 1or.. ..i mn>:. e.g. lhc pock•g:
mm: •nl 1hc class nunc Yoo can odJ • • s1.11<11\:fti i>r cbssd or pock"I'$ -
)""ebss Ii:. 1<lta:h 1Do1< )OU 1011'.iC 1h: rcb1cd cbsscs 11 >"" code "11houl 1hc
p'.K'lDJIC qu.1•f1<r

.;, tatic in1po11s


Sllllic ll'l'Ofl "1t:at....: d1:1t • ll• •.. , .,.,.. (fickh and n>:lhod>)"""h att dclftcl 11 a
cbs> ...,, the p c •• occcss 1mdifk'r 10 be lticd n
Ja\..
lhccb.. n "hoch
code wai- lhc rn-.n'i>cl' "dcfn.'<l 111: t: t....: pro'idcs a 1)11<"llli:
>11«dYina
ncblc corotu'b
ll\".CMnl>n1 10 -code ""1hol.c '"ii\: 10 "'"""""' lhc cbss d\11 or\Jiully ddincd 1hc
ficU.

More Java language constru cts

0132C5l ll l 16l
•• I nterfa ce
/111ift1ccs an: co111r.1CIS i>r wlu1 u cbss can do bll lh::y soy oolhing aboll lhc way in
which lhc cbs. nU>I do L An l111t'ef<1ce b a l)l'C s11tibr IO o cb.s. Lilcc a cbss un
if1.4T£tcc dctn:s 1n:l00d:s. An Ucrflcc can ha\"c only obstn1cl 11..::thod.s. no concrccc
nli!thods an: d>wcd. Methods dcli>cd i1 dcreic.,,. an: by dcfaull pubi: "nd ubstrnc1
c : q dcci:1111tion or ''"'"" modifier.. i< op1ional ln1crilcc• can """ consuu.. which
"" an: fn1>1lclly r>ooi:.>1311: and lilll. A cb» c.>n fn1>1cn>!IU 11n iwcrilcc. In
... cusc
d> ITUS1 provDc concrete i11JL-m:!Uatiolti or the OUCriilCC nl:lhods. If you override a
' '" i
rnclhod defiled by un 111cr )'OU con ubo ""' the tovu nd• Ola-tiol\ 1k
k>llo"•ing ca<lc hoY.'S l1n c:<a111J': -.,,1cnl.".1'141tOn of 1111 itt.crflcc arxl il.s
>usap: y.'iJlil 11 cbss.

P\lbllc 1nterface MyOefLnltlon


I t Je• n
Strtnq UllL•' Attp ://wwv .vo9ella . COft"1

1 t ne vec• -. h t
vo- 1<.I t•1t C J 1
void wrlte Strlnq e):

public void teat C)


• b
'
A t 9 n

public void wrlte(St r Ln9 ••


f A t n b

I
Class met hods and class va ria bles
Cbss rnclhods and cbss \nmbl'$ '"" assoc:a1cd wilh lhc cbss 11nd no1 an instance
ort i>! dis.. Le. obj<'CIS. To n: '< 10 lhc;c clc11-... ,. )'OU c11n i..c 1hc cl"m"' t111d a
dot (".") IOll>wcd by lhc cbss 11>:1ho<I or chss'"rilblc ron>:. Class mcll'ods 11nd
cb.ss v:u·ilbio;-s ""' d<ebrcd """h 1hc 1tat1c cywonl. Cb>s n>'.lhods arc oM>
cal'.d "'"' m tlt!Xh und cbss vnriabk:s areacal.".d :1101it· ·url<1hfnor11c1tit·fi /1ls.

An Cl([ll11'lc i>r the US. jlC of 0 Sl31ic lielcl i< punt ln ol' lhc IOlowing Sllllcll\!11C
Sy t.e . ut. .p nt. n ( "H l lo Wor d"). •Ut b 8 suitlC lick!. 311 ObJCCI O( type:

0132csl 11116

I
••..istr •m. and )OU cal chc pr n In c nrchod on dU> objcci. If)'OU dclinc a
scmoc
,.,rob\) doc J•"' r1111••"'1 cmTC>m..,,. ••MX•llcs one cb>' \:in.:1bll r 1 cb-> no
nullor
I•"' nlll1)' i>.tanccs (objecb) .,..,... The ot:11oc ""'bl: """ therci>n: be '""'" as " po"'11
\'aroblc. 11ic •• l •·J• ai
""cng codedorro1i:.tra1c< chc "'"llD or
ntr .l
'" • fieUs.

•t•tlc vold t.••t C J I


lytt..out.pr Jntln t•noll ••1

pYbl le attt lc vold ..sn rst rl n9(J •rq•• f


ly•t-.out. .pr lnt1n CHyStatlcEx•e (.11•. Pl.A f"HOLIJF.ftl :
HyStat l F.xl .teat C l 1
I

rr 1• riibl: . u ti;: dcfilo.'ll lb ud."."Cbre 111'111 tJie m'il lhC I n•


eor;;h1111.
c)word. 1110 >tntoc method nn1 "lhoia any l'l<Ulncc or the, .. IC4MOI dattdy
OCC(:l\S 1..,....t;uc \Olmb.s orn
cbs •
•• Abslract class and 111ct hods
A clbs IU1d 1n... an be decbred b at t 1• An t t a t cb-"• can not be
dft."Clly i>.WICCCcd . If I cbs; OOS II bsl one 1'"'1hod \\fli:h only C0111011 tic
dcckltallOn ordio n>'1hod. buc noc the i'1Jl<n..,,..cion d>.:n ch.. ct1-.s ;..
' ' and can no1 be irtiwicilccd. Stb-cbsscs need then co define the rn::chods cwqit
if dq arc al.o decbrcd ft abs1111c1. I( a ch» coruii:. an ftb.nc1 n\!lhod t 1bo need.; co
fict d<:fincd '"'h chc k<')'\\'Ord b t oct.The 5A>wina CW11llc showsnn
abs111K1class.

publlc abatraet cl•• KyAbatcactClaaa


•b• r•ct cloubl• rr•tu1nDouble 1) 1

\Vorid ng \Vilh Classes

01Jics1 11116 I
Table 2

\\'bal 10 do Ho''todo it
package
test;
Create • rew cbss cal"l<I MyNn.Cla.u. public cl• HyNewCl••

racka9e teat:
Cmttc a new 01 (im:lncc varilbli:)
calctt 1 ar/ of lypc Slrils ii1bc My•ewClass pblic class MyNeWC ass
cbss privet• Str in9 v•rl ;

ubl1e cl ss NewCltts
Create a Cotor ilr )Ollr MyNeVClass ptivate String varl :

cbss whi:h Im •sparnn-.:tcr tlnd •ssUti public KyNPw('laaatS


the \'3 of i to the var! mstancc v:.t.Ub 4 trlnq
paral ) f
varl • p•ral ;
h •. •r 1 •r•l :

rck•9•test ;
public class
MyNewClass
private Strln9 v•rl:
public
yNevClass(Strinq
Create a 11C\Y m:tJxxt cal:d doSo111eTh1i ra 1vorl
J C• parel ;
1g iJ r t •· • p •l ;
cbss whi:h docs not return a,,..., ond
Im00 p;unrrek:rS
1)'0<I'

public void d0$09CtThiftCJf)

public class
private Strlnq v•rl z
HyNewClasa
Create a JlC\Y tn.!lhod cab! doSo111eThi11gl il
)'O<I' cbss which docs not return •1'3llc public
KyNewCla•11Strin9
ms I\\'O p:irnnrters. a Du and a
tlnd ra l)varlC • por•l ;
Person t J • •r •l:

public void
doSo.eThlaotl

0132csl 11116 I
\\liar 10 do Ho"'10 do ii

pUOJ lc woid
OSOM"Th lftq2 ( lri t
tlOlll) f
• Perscin

*
I
t•lt:

blic cl••MyewCl•• C
private Stt lrw, varl:
public KyN•vCl••IScrtnt
ra il I
••rl • p111ral:
r t .1•.va r praJ :

pub ic void doS Thlftq(I

Create a rrethDd a.led doSomeT1ti1igl m


)'OUI' elm "hi:h n:uns on n ,..u, and hos
d'ltt IWO Srmgs :ind 3P<iSOn public YOid
o..ThiftflJIi nt •· Person
r1on) I

public t rit
S011eThl119ltScln9 •
trinq b.Per.on peraon)
return ; y V4
r:. • e "
w J dO ( r e
I
I
test;

l c class MyOtherClasa
Stri n9 .yvalue:
C,...rc a cbss calbl MyOllt<rCla.r> ,.... .,.o
n.wcc ,,.,w,1es. One ,.ii sron:a sure. °""
doq;
public Strl"9
tMyval C I (
!he
i>r return
orher ,.ii .,_a Dog. Cn::ire p:t1<1" :ind $C'tltt
\",llill>les. ayvalt1•1
public wold
setMyval e (Str.ng
yv -e)

0132csl 11116 I
,,
[Whal lo do flow 10 do II ,,

I ubllc void
0Sometht n92 C int •· Pe r1on
pera onJ l

ubl lc claal HyNewClaaa


I
l't lv•t1.11 S:tr Lnq v•r l:

public
KyN•w(lo11Cltrtn9
t'o l l C r hJ•.v.n r I a r•.1
v•r l • p,i.ra l 1

public void doSomeTh ln9C J

ctcaoown-cthod cull'"<I tl0So111tt17ri11g1 l


)'OW elm which rclil'I>< on hu \':!IC and lus
11..,., 11aron1CI._..,., two St public void
ard a Pm;on oSomitThinq2Clnt a,
Par1on
pereon) I

public int
do 0"'4thin?JCSrift a,
St rln9 1> , l'or1ein rertonl (
r•tur n :
Any Vd e
v J d f
r ti! • •• I' •
I I
- ·f ck•9•
•1\.1
public
cl••H)'OtherCl••t
ere.lie Dd1» cull'.tl MyOtl1t•rC'h1S.f "11h St rlnq •yvtt.l ue:
l"O r1St111Cc '11ri:1blcs. 01ic wil s1orc n Sltlij!. l>oq tJoq:
public Strin9
the otr \\ii tore u l)og. CrcJlc Her and qetHyva lu•C l I
scncr IOr llic<e'111ilblcs. r•turn rnyv•l
I
ue1
I publlc:T void
etMyvalueCStrlng•yvalue)

OIJ2csl I I I 16 I

You might also like