Javacw

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

TAVA

Buzz Wonds (ox) Fertunu


Simple
JAVA ns
desghed o be casy
-te Rean
or pufesional F nus.
and ue
ectvey
8 ricd CHicnted 7he object medal in JAUA % siple and
easy extnd.
R e b u t »The multi - plodbfovm eviuenmtnt pyovicdecl by TAVA

enoble tr cMede new


applcaticns
)Ouiti-Tiveaded JAVA Appurts rulti ttoteondeol pusanmg
which oeus o wibe psqiouns that con de may T g s

simtroneudy.
Achitece Nauknal TAVA PA®ndes coole Songi wdily and
Pectobilky. t helps te eopily pgode the opuatung
tepeted ane High Payr mute JAVA enabes onestzbn
irvvoneliate
A pltum pogans by Cmpiling trte
esudetuon calld SAVA byte cede. This co de can be exetuted

y ystm that inplanets TAVA viztual mawine (JyM)

DDIstibuted ^JAVA dlaigned foY te distvibuted


Enviumavt o hu intent became handes TcP/TP
Pretoce.
J A V A ÞYeqms car wifh them sulostartinA
reut
un-tme type infevmaluen i.e.. wed vob
Yet
on e netokve access te
ebject at un time
300PS me
DEntapsuladion t he mechwwsm that binds tojetke

Code d data it manipulotes anol kesps bst s

Dm 6utside inteylerante. and misue


obiec#
A a poce
by which
by which Cne
one
ojec taure t,
nhoutance
itspe r
t speYG the
the' tneept
enced
onethe object
puopevkies
heinachical classihraton.
D Leati
> tmeans a n
& siA aa
eotune t
3olymexphism
3) doue at
be wsed eY genetal oactin
allews one intuface
be wed £or wlta purpose.
That is ome bject con

ekmert o ooP. Tt heht


Atsbraction is on esserdiol
and providung ony h
hioleteunmecessary. informdion
needed info'rmauon tthe wse.
insrmoton e
a23 Aroay in JAVA

Auray in AVA a
Aup ike -yped yootiable
stued a common nam n JAVA allovays we

dynamicaWy alllo cated.Amays axe seoied n contiguaus


memoy ocotuon. Since amays ae bjets tn JAVA C

Can nel tthe ength uAing the ohjet prepedy ength


A jNo a y vovuable can alue be cdecloned ike tier
vaiables.The vauables un
tthe aeay ae exclened and
eah has an indee beginnng pum zee Thesixe f an
aay ms be speciped by int eshact valne and net

Ne can andamy access h e ellments o on ae

Tha s on ooay cannet e attored. Thore ae 2 types

One dimenstonal aay


) Muti- dimensiomal avay
Jhe q al Jem Gne dimenstomal atay deelarAMAn

type varoune IJ;


Here, type dekanes e elemeit bype w a n awray. The ekmet

YPR dekounines the daa tpe each elmurt that

Cempes hw atay
Whun n away s deckavasl-iy a

Heerence on oveay is Uaed, e gve memey Spece te

aUay
YOvtnanme =
newr type si«]
a =
neus irt |5J
inta
Combined> i t al]= hew int 5];
memoypace t -
Hene the nw epeHatose
alecates
e
a
in JAVA
ritializing aay
inibiclize away dning deelaxat
Dn JAVA twe con

bnt a S1,2,3.4,5?s
in JAVA USng e indes
tWe can alse intialixe axray n

l0 j
Ex alo] =

a[g 20,
a TaJ a0

aues the eltmetu aay in JAVA we on use

The index nmber

Sorple Puoaam
impe java. lo. *
ckass somplu i
puki static veid roin (Stri angs
bo a neuw üt |5;
tnt o {,2,3,4, 5
4piv i= 0; i4
a. lenath , i+a)
Systam. oud.. pairtin (a[1),
Mutti-TDimonsinol Assa4
n
aay t c a . A consists f Jows and
coums. Also called modbix Ooay
Fx int
a[]I]- nw it []E];
a[ofoJ =10

Sample Progam
impet java.is, *

hass ezarnple t
dette
pubkevoid mawn String aqs II)

b t alT1- nuw irt [3]3]J

e int i=o, i<3, itt)

fo (it j0 j<35 j++)

)
Systen. eut.purt (a [JIJ+

System. 0uk. puirdkn )


3
CLass in JAVA
he Rayorol clau A der
Ris deelaned by using
have tomme pioputies. TE
a gcoup o object whih
upon
whith ertire JAVn longuage
the Logial onrlunct
ho shope ovnd hatone-an obje dt
bruilt because it clt,nu
lass olnitton is as lolout.
Gteneral fexm

ckass olauname
ype instante vaviablel

ist) S {
type mehedname!(paroneter

methednamen
3
The doda oY vaviables deine widhin a clasz ane Lallesd

Treton ce tanee vaviols because eah stante e a d a


Vaioble
Cortoirs te oen copy these voviable. Coectiveky.
eAhds vavuwobls ddined widfin a a s ae cal

Menbes t e da%
Exarple:

ind heighd;
vt, depth
Void yownel)

elAn wicth * Phuight depth


box b nw boxt);
b wic
. hetct
To eode a das obpt
elossnome obietnorne =
assnorne
neu
Ex ),
box b =
nw box C
mote, b wil be instauee bex.
aes he vaniables we we dot operots Y, ha dst
eperotos Xinks the naume a the ebject with the name
instunae varualXe
o gve volue
b. width -
10

Sample Pangran:
ckoss be
arc &enth
irnt bradth
ivb meigt
3
elass deme9
public static veid maun
(Strèng ags I3)
bex b neur box) ;
Juble yel
b. Rength = 10
b. bxeacdth =5
b. hetght = 15,

vek b. ength * b. bteacth * b. height ;

Syetem. 6ud.paürtd " vølume cubeid: + v o l )


Constuuctoa in JAVA

consbuutoi iniiclires
an object îmediatuliy upon
A th dan nome
nume as
some
Creaun. hag the conutuuctoe u
he
nesides. Once delind
in which it Caatid. Conatruct
is
whn the okject
wtamatiially calld veld. shis b bdtoue
evan
noun kpe net
o n t have oa is the dae
dan n u t u
he impliit
netucun type
inibialixe the
inibialire he
Coneduuctn n
be used to
type itsul vracUbn. SE Cen alo be
nstounte vauiakes duing object
e poguamming which has te
e d te wribe tre logie
the eotuon f ter objedt
e cxeuted autemsicaly en

7he ane 2 p Cnsbrtor ;


) Delauit cervtructor

#)Pavomettrized comstrute

dlout
A covtructor ohih hos ne poutauneters iu ealled

cTkuteA,. Dt called whn an instounce a


olloult value
cHEded smainly uled or asiqn
tthe neudy txeaked ohjet
ktorree:
ela box

Systen. eut pirtln ( "No paarnekos


),

pubit stetit veid mav (Sing aqTT) i


box b new o
Pavtam etoite
s cald
epeuamtun
Aconstuctor tohiulh hos speilic ro

yolluss
constuuster. Tt s wsd te plovide dilent
vamtuid

to distenct ohjects.
Examae

class sadnt
t id
string mume
bund
studert int i, sing n) P cotru Tor

id =i ,
name = n3

void display t)
+ name);
"+ id Name:
Systurn.ouk.pxötin " Td : +

publie stdic veid má (String ags I ) ?

sudrt 81 -
nw studnt (101, "olivet ")
sudert 2 = nuw student (102, " Tina")
81.display);
B2.displag)

d: 10 Noame: oive

Nme: Tina
Id 102
Cnatuuctex Oveloading c o n s t r u d o L

oe
ian
moHe

y th
s a technique f having dyfuetieted
ane

pvanmetu list.
Ty and
tiin
wth lfueyt list
in the puameuu

by e
no
compder
than
cn JAVA
Metta Ovuloasung have
ore

ulole3 a
class to
thot allous payamster
featine dilerut
a name
buut w h
wth the scne

mechansm
n e metlod 2
sveleading
teugh
mhod
AVA 6ppo vt
peTmeten
ehnging the no
Metkiod-
G) By

class adlition ?
irt b)
idt add ( i t
a,
stostic

pekion Cotb)s
b it )
etotie it add (int a,int
ueturn (atbtc) ;

3
clous test
stolie void mair (tring axg [J)
public
add (to,20));
Systom. 8ut. prirth ( addition.
add C5,10, 15));
Systen. cut. puirtln ( aeldition.
Nethod-2

By changins the data type


Ex

class addition
ptotie irnt add ( int a, int b)
rtn (a+b);

stoctie doutle add (deude a, deule b)


t u n (a+b);

3
dass test
pulie sbotic vecd main (Sreng ags I7)
Systen. eut prirnttn (odolition, add (a0.5o)) ,
System ed. pxatr (addition. add (s.1o))

se method evdsading Advanboge


heps to
inuease the xeadibilty the progam.

anh oitanne in JAVA


s meharism
a
in wohich one object app@ss all the
popetics and behavtewn a
atert object.t n e
he eey ouiented poganming
object hat alteus
Cete a hew lass em an e xisting slass. TK newr

CHeated knewn as duived edass, sub


ckass hild dass. The existrg elass e m whone tte ohikol

Sanes is leuived is khown as base lan. paient c{os

supo kass.
The etehds keywod is wd to h eibante
plosm in
JAVA
tax
ckoss baseelass

Mcoele gsu hxe-metfec

eKass oleiredlas ekerds boseckal3

metbhels

Supe Keuvold en JAVA

in java e n e vavaLle
which u
ed t
upex kycooxol a

9c immediake potent ilas ebjedt


Uses Super kequwoAd
nstane
Ltcan be secd te pe immecdiote pootet claus
Vaniable

Excumple
cdaus auendass
int n=/o0 ,

das subdass etends super doss


int n I10
veid ppunt Of ue stem.et. psittn
ov pareuk da
(3upex n
Systern out. pairtn (n)

public siatie veid main (Stng ags L1)9

Subdas m = neur sublos[);

m.pi nt ) ;
c a n be wsed to invoke
immediat peret
pocrert Jo
methoel

clas peoson i
ved ahewC)

Sustem .eut. painttn(" This s base as

las studut extends


pesen {
veid shew ()

ytem.eut. paittn "This duived das ")


void display ()

Bup.sheu ()
F

doss Geme
public static voicd man (Strirg args ID
suderm = new sudent O;
m. )

Osput
This boe elas
Thits ts olived las
ECan be used te aele immediote tastet dars consouuer.

EX

cass pesen t
peUsDn construudox

(" Poesen ckass


ten.cut.puúnttn

extenols peruseni
ekass student

studut ( )
Bupe () ckas
censtauctex");

('Studert
puirtin
Systern.eut.

3
sbatic vecd main (Sting ags IJ
public
sandent D,
student m = new

Detthed vevriding
delaned in he paret
Aubelas has the
bame m etbhed os

Ka t knun as mtthod even Hiding n Java

2heikic imrplsrutatuen h mithe


wed to puovide
patert &ax
o i
.

hot has ben dedaed hy ome

t d or u n time pelymephism .

Ruls to be fsllewed

m u t have Barne nome as irn parent has

r u s t have &ame no ? poAun eten aas


U neten
in þarteht Ao.
irho

3Tue must be S-A Hdalionship


lol23
Dynamie Melhod Dispodch
i the methnd mechaniam by which
voidden mehed
a
calto an c
Mesolved at uun time Hatbha thon
cepile
compile
tume. Dynamic methocd dapatch s
impetart kawe tha is
hew java impleneut rwn ne
pin cip
Superdo oymenphióm. Java ses

rptdkas relene Yoriable can


na to
subdass
subdass objet, te
Hesewe ealls to
eve ddon methods a
uwn time.
uhun a
po das renene is sed wed te ccal an
evettidden meked java detouninss whih
vesion ot thot
metfiedezeute boskd yon he
type o e bjeck Leing
7oenes t at he time co
UsS Pynamic method dupoth:
allewd, Java e pperct ovovuding methods whih
bue
impevtant ek un tine pelymehism
D allawsa das te dline muthods thot w
be ghared
y oll tt douved as.
Taleus sublas te inoepscte thwr
eun mathod and
cekne thei implementation.
REWRSION:
Java &uppe ne cuvtsien. Recwutem u the
prtocek
depining &omding in -toums bseTn java puogaenung
teuwuion the atibute thot alleus a mathed te Cak

bsea mtwd thot cells itselt s satd to be znewsLVe


das ocdesuiol $
int ecet(ut n)
int esut ;

n==t)
etin
esult = lact (n-1)*ns

ewn eslt s

ciass reuien
pislic stokie veid main (Stirg ags TI)

akertial new a cbosial

Systern.eut.puintin (" Factericl 3 "+4 fact (3));


Systen. aut. pairttn (" Factevial ois:" foct (»))s
Systern. eut. paintn (" Facdeuil s s: "+. Ract (5))

Whan
fat is colled
fact () Rectmethed], The fot s CA
wm an
cotqumet eone.h e aundion etuwuns ene othetwse
t nds
puoduct fat (n->n, Ttis precen p
diX n
eguals te nethed
ont. n I and the cells to the
being tusuning
when
yeu comyauke t fecteuuol 'of 3 Aicst call
fett () wl pas a
Rcond coll te be made
made
within

avgurnant2. u col will pass fact () te e caded


tind time, with an
angunmet One
i s Cal eiuetusn | which is thn mukiplies by 2
Ths Heslt is s tetwvud te e euginel inyccadien

e act (). This yield te answe 6.

las hi keyweno
a Akene vai aske hat nfeus to c u n t okject.

tie
The mest commen use
this keywotd t o eliminate
Corus icn between alas atbibute and paramtby wih h e
Sme name. ettu ses trkeuwend is as ollus:

)It i usd to Ke cwet class instone yavdabJe

2 T is wed to invoke went lass methed

a) It i used te inveke wurt clas cernucto.


ca.
an be ased as a n arqumut n te mathed
5 ) t can Le pased as an asugunent in he constuule cau

Example
Wbed
er elesing cume das istan ce vasui able
das sudert S
int Holn6 ; Sbting rame

String nome
sadut ( int ndllne, Sting name)

u.uone =neWne,
this. name = name

veld diuplay ) f
Syston. out. privtkn (Aolno t +name
cas deme
pubsle statio maivn (Stuing avq I J)
veid
Sttdent 8 = new studet (10, "Guita");
stradent 82 studot ( 101, "4bi ") ,
ne
olP 10o Guta
1. display ()
to Abi
82. diplay O

lass mthod
sed te nveke cunent

has Af
veid m )
Hello n );
Syston.u. prirttm

veid nl)
System-eut. prirtv ("Helle n ");
ths.m( )

pul-ic svdic veid main (String


A a
ovgs [)
new A)
an()

e N
Ased te invoke cwmenb kons conudten

clas A

prürtin('" Helle a')


Systen.ed.

A (int ) {

System. eut. prirtln (z) ;

3
dasg derme
puic sotic veid main (Sbiwg avgs [j)
A
A a nuw A (i0);

upt
Helle A

You might also like