0% found this document useful (0 votes)
3 views

Java Notes

The document discusses Java packages, their definitions, and how they facilitate organization and encapsulation of classes and interfaces. It explains the rules for creating packages, access modifiers, and the use of import statements to include classes from different packages. Additionally, it covers interfaces, their implementation, and the concept of nested interfaces in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Notes

The document discusses Java packages, their definitions, and how they facilitate organization and encapsulation of classes and interfaces. It explains the rules for creating packages, access modifiers, and the use of import statements to include classes from different packages. Additionally, it covers interfaces, their implementation, and the concept of nested interfaces in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 174

Scanned with ACE Scanner

Scanned with ACE Scanner


Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
Scanned with ACE Scanner
UNIT

PART

packqe
java doesn support prepoeesDY ditectives (tfnelde)
’java
Generally to e classes from ofbr poqrams withtat Copn9 physicay
thà can be done i qova by msng packaaqea
’ packages prvides way to achteve yewsabiliky
a

Definatièn packaqa are ontains Jor clhuses and viterkaea.


pack aqes can be cakqvizd nto the too fuvmi
() predefine (o1) Built Packaqea
c) aserdefkined packaqe
are

() ao t (absfract wndow toot lit )


u) beans

(19 to CPnput and Otpats)


) tanq (lan qaqe)
ui) Math
tn) nei Coehiork)
(Vee) nio (neo lo)

(w) Ymi (Redte methvd fovoation)


(x)
Security
(K) tet
(ou)time
(n) fl (utilety)
’ uer de7ined packaqes are cveated by the pmgrame
Berifik of packaqe
) The clases containcd n packaqes Can be sed

(e) Java packaqe , ç wed to Catcqoie the clayses and tntataces so


that they can b easty mantained.
(or)
3) Package provides aces pokecton ie, prtveoting bther proq
packaqes tom acceAsing cluses bat ave meant ntesnal we
(9) Remgves naminq Cotlison:
Lat (ava .aot*;
Rules to oreat aa packag
) DeclaYe the pack aqe at the b quninq Df a jle.Gieneral km Df
pockaqe siakmerd in packaqe packaqemot ;
package name 3
freccecdig y "pockaqe" kupoord
classes declared wifbin that gde ill beleng to that speciie package
+ packaqe statcment is not inchudes then classes are Storec ín
def awt packaqe ohèh har no hame
bejine the cles that is to be put the packoqe and decare t as

"pabit
Cxeate a subdirechy to sfore clas leu, Subdirechry name mut be
(ame as pockaqe Dame
Compile te le [o that elas de iu Ltoreol subdiseetoy

pblie clas one

pablt vod diplaye)


44
Save the proqram subdirecty e, Rrst
C) D: Cveale aa Suhdirechy
Create
kubdirechy name mwt be Same as
pockaqe
D:Imd Fret
D: led 1iret
D: | irst \one. java.
One qava to qenenale cla« lle o Suhdireckoiy
( Then Compile one-

D:lj'sf\ gavac one. ava


Compiks Suceess tay and qenerekes. clau jde n HSubdirectony
Cveat anofher claus
Pockoqe FKt;
pubie class Too

pubt void thow)

Syskem.ot. pintnc Metbod tn two clau"))

Acces proie etubo +


’ dauses and packaqes are buth mean of encapsul aBg and
Containunq the name kpace ana Sope of variables and method.
pockaqes Ore Contaunrs Dt clases and Sub
Private No rnodifiew Protecte d Pubic
Kame clrs Yes

Same package No
subclas
Yes Yes Yey
Same pakage
Don- Sublas Yes Yes
Dfereot pockage NO
subclas les
ifereot packaqe No No No Yes
non-uhas

Aava inehdes the npot Slacment to bing Crea! ckeat certaio, clauer oY
entire package into visEify
Dmport attmenl is a conveoieot to the
proqramex ies pro qrammer
te ehnieay nof voriing Complek
proaram.
Do java Souvce jde ,import Sakement are
joeloTed by packaqe
Statement
Gener a tm of tfe imprt
ünprrt pockaye - Dame, clau Dame (or)

inport pockoqe name .*3


lyincludes all clase
Example
Packaqe tcond;
pubt clau ci

pablit vod display )

packaqe decond;
pubat class cg

pubit veid Sow)

Sytem ont.pintso(" cias C");

mput econd
elas packago
pubie slatic void main
CSting argst)

Ca (2 0t

Ca obj. Show ()3


for bill in ackoqes
chpnt ava-ulél.a;
claus scan

publie Sfatic void tman (sting avqsl 1)

Scannr S- Due Seanner (syikmin);


qutem but pöntin (" tka x value";

UNIT

mauipte chbhesitanee tjicutt and


Po ctt mydemectation of
tdlds tomplerihy to the lanqoqe
as Intafae to
Tava pronides an altexnats ppoach known
Suppt the tonept of mtiple bbeitaoe
D4iiq an Toteqace
Sunila o clauea, bwt
are kuyntacteaty
metbds are declare
tack of natance Vayiabls and their
methyd.
oithout bydy ie, abshact
Pntatare tomans variabe hat are declared as jinal and
Hatit are aleclared thont
Tn itngoces methud are not dekined ,tzy
bvdy implernenk an lokgae to
the neprnibilhy of the dap,that
- Tmethu
defene tthe Code npiementotion of these
Qnd
ar ne clas Can
can fmplement an ntckace
Any no-of clasen
inplement any no. tf inkerkoces. re
method and variabes
an ilenface is cdeclared a 'pubtie' au
au methodi

imptchy pastie.
syntan t access inace nter7ace-name

type finas variabler >value;


vetuun type methodu ();

public itenface Ttm


Esample
State nal t e> \00

vecd displ ay

mäTe else Can imple


Once the nt7aces s de7incd
ment that nteaface.
clas di
a cten
Can alo 'nlersac implement Canan claus yA
one than more
Show)} obj.
1) arqs Shing mai(n void sfoatit pusie
2oace clas
shvod pubki
fmplernenk Dokr clas
Firt
boo()3 old pubie
t03 ip nttenal Sfatii
Frsteke7ace Example
cels; of
Bucy
{
Dotukotenarne implemen dau &ynta
name claus
Syntant class clas name entndsSnpr claI implenen
interkoee s,inkrjoe 2
bvcy of clau;
a clau
imptemenkd by
he methds bf vokertace that are to be
mwst be dlectaTed as
"pubtie"
Fande elas Teut

veid dispiay)
methyd in clan 'A"):
Display
Syoem. ut ntin ("

tnterface Potr

pbic voLd shoo( ):

ntoface

þubec votd berno();

closs sub enkend Test ioplemens otrt, Pnkr 2

pubuc veid shyw )


Method");
System: uk pintin" ntaface 4
public votd Demo t)

Syukem
System ot. pintin j)s

Demo
elavs Dnkntace

Saus padic statie void main (Sting orqt1)

bub subv

O/P% isplay in class


PoterfaceA metbod

Potgace 2 method
(e20.
Neskd
otbin another nterace 0r clas is
intenface ie, declared
koon as nested tntotaee
are mico to elate d inteofaces so that
y Phe neted Bextaces
Can be casy to masBain.
ntnface trust he veferrecl by thx buttr lnkr7ace
- qhe natkd class
48

’ carnof be acesed alirecty


(Netcd nkntaces ane weod to qroup related nttaces so -thaf they
Can be easy to aintain
inter7oce
Nested nknace mwst be pudit it deciared initde tie
decared oithin the clas
bwt * Can have any acces moditer
oith n nter ace
syntan of nekd otrtace which is deciared
inteface . nkface -Dame

nderface nued ntaface-name

ntufoce ohern declared aoithi the clan


&yntas of neuted
elas claus-Name

inkesface Neakd nen7ace

Enampte tntea7ace voth in the bterface


tnkrt ace &hoable

Void soply}
Lokrfae Mexs aqe

vod Msqcs
las Nestedtnkvface 1 implernenk Showable.Meaqe

pablie void miqc

ePmplermeotq the nekeol tnkafae");


System ut nint in
pwhic void sho()
&howble")

3
clas Tet Neste d okface

static vetd main (Shinq arqst )


pwblt

Nesicd tnifare cbj - neuo Neskd okfacel);

Qoplementhq nestcd tbinface


Podementing otertace thowable

Pntfae voith in the clas


Enampde
l a s Dem o

pubut tntrsace Nskd


puhui votd neat
49

clas et iroplemenk oemo. Nexte d

puhat votd nentC)

Sykrn: out pint in ( Tmdementinq Neakd 2nerface in


Demo');

clas Nested Dnkntact

pablie statit void main (Shing orqc)

Neited Bnteaface so clas


Demp.

Ohtput s nplementh
bpey Poknfae
adejkied it Can be fimpienentd by anY
y nee an
numb Df claes

nkofoce Ppphycnq Toteface


pwbi void rea0S

elas cicle, ioplemenk pplyinm Potace


double pi= 3l4}
Aonble To2, area of cirde
pubt void areac)

avea of crcle - pi r* YS
-"+ area of circle);
of circle
sysfem- bn pintn ( Area

clas dquare inpdernent Appyingooface


nit a=5, area of Sqnare ;
pubut vod aveal)

area uf square + area


päntlol Area f sqare

Rechanale froplemenk Appying Pokiface


clau

Pubit void areac)

area of rec)s
ao C"Area of Recangle = t

elas Acas

f
publie sfatic void ( stinq argse1)
So

Square Kquares
Rectangle ()3
Reclanole

areal);

l2-*6
Ontput $ Area of circle
Area of &quare
-2vs
Area of Rectangle
Nari abes i loerfaces and enterndhg mterfaeu t
are avadable to any clau
The Conitant values dleciared n inkafa
that implenenk the inkaface
voviakdes
ntegace
nt m2 (0, Dz|S)
Puic void shovoC);

clax B ?mlements Yariables

pubt void Showc)

Syctem:t-pintin (ms4n"4 mtn);

lous arpemo

nùn
pulie sfatie void man (Sfing arqcc1)
b Show);

’ An nkoce can eaknd anotfe


tenface in the Same way that a
lass can enteod anofbew clas.
The "entcn keyword i secd to ntend an întonface and the chld
nknface # obei tt the methods of
theparent nkaface.
nteyace pavent
Sfatie hnas mte-l0;
udi'e soid shoot);

nerfae cheld entends parcnt

puie void diilay (;


class DDimplemeot child

polie void thowt)

Sykem. ontprint n i+);


Syskm oud pintnnj ) :
Syckem.D pinttn ( ks"4k);

class Pnkodace Exkeod

puhtie sfatic
publir ved main (Sting argsc)

d. show(0;
d. diplaye):

Ontpud i-t0

kz20D

You might also like