Java Part 1
Java Part 1
PhOaban
gland Alone Applieetion :Thad nunse
mwm mwmmwnmm on a
Single machine.
web Aprlicoian That hung on 9evenal machines.
Documentation Section-
T deschibes a bnief descbiphon about tha
Phogpam I4 Can be bitten using eommetline.
7(Single Cotmment Line)
/ . 4 / (Mali Cemmet Line)
PackogeSection
T deHnes a 9et ef easses and melhods
(fancions) hat can be used in a Pbeaham asohen
hequined
. Impobt lalemenH
T is used to include an aseh-deltned packag
in he Phegnol
.Intenface Deiniisn
T an atenaive of mulfiple inhenitence.
Useh-defines elasg De-intton
denes he behaviouh and eha.
ebistics (vasiables) ol on abject ahan
Main Methes clas
T detnes he object ob te
useh-
deied olass, T is the stabHng Point of
dny java
Pharom
CoMputen Meneny
9eubce CodeGJau aa CanpileniosubyBe aode &clas
Pin-tin9 JAVA
0- values in
Thene ahe tdo me thods o pntnt values in
Java. Ihe methods abe
Dpint)unchon
The pminBt) Funchion is used to phint values
The
nd
nd afe
afHen pnining the Cub3eh Oill bemain in 3o.me
line
next )
Dalotypeg
Dalatype nefens o ype o value
to a van iable
.Qlaaikcalieng
DATAT YPE
Pnemitive/Basic Denived
Usen-deined
Fundamental
Elementar Aproy clasg
Funchion indenae
Numeni .
Non-imebie
Inlegen Floahng ehaacten Boolean
Poin+/ Rea cha
byle boolean
ghebd Ploa
in dleuble
\on
Paimihve Dalakype
byle
MwnmmmeDaBatre: A byle dataty pe is usel o declae
an integen vaniable hat needs
byBe of memory
The ange of value tha- can be
Stoned i - t o +-1).
Syntan byte vabiabe-name = value;
Example- byle d-5;
5
he ange of value ha an be
sBoed is-5 to +(2).
ymak shent vahiable-ha =value;
Exanple- ghent a- 50;
Komple
elass A
in a=l0
void fnO
SysHem.out phid (a)
DObjecl
An sbject ig an insBance of a class. j i s a
Hhig ha must have he folloain 3 phepenties
Tdenh Acadion value/1deny uiue idanhil)
Look/ Face|Feel
DBehavioun/ Fuchon
yntox elasoname objel-name = ned constnuctob(),
lass Membeb
The vaniable and the method declabed on defined
elass is caled Class Membeh.
OHhin a
The vaniables ahe ealled Membes vaniable/ Tnslace
Vanlable
The melhods ahe called Member melkods.
Exole-
Class A
n a-1o; II
void Membeb/Tnslance vanable
h) UMembeh Methos
Syslen.out. pnind (a); oass Mo
Chabaslenisie/Propenhies
has Some name as hat of class nane.
T has
hetuntype.
ho
.Qlasificadion
Consthactoh
Delault
Con slnacto rahamelenized
onsthactot Con gthaclos
mt ab.e,
Dcons (Delaull Cohsheaclo
a-10;
b20
atb
9y3em.aul paih (e);
elasy DelaultConstnactoh
public slatie void main(
9thing ahgst1)
Dcons obj =nea Deonst);/* T antoaically call
the delault Conshach
Example-
impobt Java.ío.
elass cons
i l a,b onstnaclm
econs (inl Pinda) !/ Pahameleized
b-
ConsBhacfey
Cons Ccens ob) // Gy
a-ob.a
Obb
3
void display ()
3
class Capy Cansthacbh
public staic void main (Shing agsti
Ccons oji -hedd Ccons Cio,20) J il) Call
Pahameleited Gonshy
ebjd. displayey
Caons abj- ne Ccons(obj 4); /*Ikoill call ay
objo. alisphy ) Conslhoelo
1
Constnucton Ovenloadira/ Ovonloade d Conslnuctob
Usina tdo On mone
Consinuston-
havna Plenen 9thuclune
hav con9tnucton in clasg
is oalled
a
Constnucton Ovenloadi
Example- Same as
Copy Congtntuctob
a b
void Pn ()
Systemout.prind (o);
Sy 3Hem.ou.phint( b);
elass MO
publie otatic void ManStning angstI)
ypes o Inheitence
TNHERITENCE
Sindle MuHiple Hybhid
Tohebrfece In henitence Inhenitexe
Multilevel Heenanchicak
Inhenidence Inhehitence
SyntaX
classhile_elas.-name exdendo abent_class-nome
IICede
Exomple-
class B exends A
H Code
Single Tnhenitenee ingle Tnhenideme nelen
Mul
cheaing
Cheaing a
hich
Pahenl olas
(A) Base clas/
elend
6)Debived oloss/child olass
Example-
Impen Java.io. X
elass A
mpo
class
indin ab;
void inpuc)
a-do, b=20,
void Cal ()
C atb
Void di9play) clas
closs 31
clas
public sBalic voiw
cunining angsL1?
B obj=ne B
obj.inpudO ebj.cale obj. disphyt
15
DMul
.Jklevel nhendence-Muiklevel Inhen.lence nefeng to
Oheai
Cheahng a ed clagg Phom an ald olagg fnom
ahich
hich toe
ode Can Cheale nolhen ne cla99 and 90
9o on
on
means heales a aenehaion ot
elames
ABase lars
enlonds
(Base of e/ Denivecd of A
elends
Denived elasy
Exonple
npobt Java.io.*
clase A
int ab
void inpul)
a-10 b-20
class B exdendg A
in C
vod Qal C)
C atb
clase exlends
void display t)
System.esupnid (e);
class mu
publie sBaic void maun(Stin ahgst1
obj.input(0
obj.cal
obj.displo;
Malkple Inhehitence Java doesnt SuPPont
Denived olass
(A Base cass
exiends extends
Example-
mpot java.io.k)
olass A
in ab
void inpuBy
a-i6 b - 20,
elas B edends A
void Pn)
e atb
3 Syslem.out pninte);
class C extends A
int d
void fnC)
da-b
System.out phint (d
1
class H1
B bji ne BC
obj. input0;
obj fn0
C obj9 n e e ) ;
oy9 inpuk U
ob 2. fn (
Single Inhenitence
Hienahchical Tnheieme
D
malliple Tkenidence
Mul4label Tnkenitence
l a e Memben:
of a elass con be accesyed
Staie Membens
thout cheaing an object
elass B
publie iaie void mainnig angs[1)
A.n)
Method Ovenpidirg
Methed Ovebniding nefens to defining aMe
o a base elass in ts debived elasy
havina 9ome
stnuctune
Example-
impont Java.io.*
elass A
voi isplay t)
3yslem.out-pbint (a);
3
olass B exlendo A
Fina Vaniable -
he vabiable ahose value eannot be chonged
in any olass i alled Final Vaniable.
Final Method
A method hieh Canhot be ovebhide in Hs
denived elass is Called Final Method
3
elass B enlenda A
void n) // Melhod ovehhidina
yslem.out prind ("DonH);
olass Fm
publio statie void main(S4irg angsti)
B obj ned BO;
Can+ Ovende
obj-Pnu; / Enhob !
Final Class
A elass ohich Qannot be inhenides Punhes
alled Fina Clas9
The KoyDond inal is used+o make aFial
olass
Example
Anal chs9 A/ Final clas
Ctackage-
n Java, packages ane te ay
8mouping avaie of elasgeg eh intenfa ces gea
.Qlassificalion-
Packoge
ubnay/ in-buutt/ Usen defined packaae
Java APT Ahckoge/
Syglem dekned
21
Main
| Java
package
packes
mpon packagerame
E-impoht Java.io.*
Usen defined ackge
ackages 4hese ae chented b P
use On Phognammen is alled Uren- by4le
PacXage ol
Syotanr elassName.java
pac koge. packnge name
publie class elaseName
Body of 4e clagg
.Accessing lackag
impont packogeNomé. elass,
impob packageName. *; l
DiagnaM
D
Paekoge
Paek
A- java
.P. jova
mpon
A Java
package Pack,
public clasy A
publie veid display()
9yslem oul.pmint (Helle Packone ),
23
P Java
impon Java.10*
impopt aeki.*,
elass P
Advaages of Package-
AJava pockage is used do Cataqonies e dasseg
and indenlaces. So +haf, iCan be easily maintained.
Java package phovides access pholecion.
Java Package emeves naming eollition
Java
sp hong
phoanom
ypes of Ennons hene ane t o pes of ehhe
hor
in Java
ni Altempting to use a
negative size of
aonay
Eception
An exception is a
Hn condion has is
CsUsed Run-ime enhon in +k pnogn
an
Prognan
du , 4 2