0% found this document useful (0 votes)
59 views25 pages

Java Notes by Genie Ashwani

The document discusses various programming concepts related to methods, constructors, and inheritance in object-oriented programming. It outlines the compatibility of return types, the role of constructors in initializing objects, and the different types of inheritance such as single, multilevel, and hybrid. Additionally, it covers access modifiers and encapsulation principles to manage visibility and security in classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views25 pages

Java Notes by Genie Ashwani

The document discusses various programming concepts related to methods, constructors, and inheritance in object-oriented programming. It outlines the compatibility of return types, the role of constructors in initializing objects, and the different types of inheritance such as single, multilevel, and hybrid. Additionally, it covers access modifiers and encapsulation principles to manage visibility and security in classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

AG Methode wtth

Return dee in jada


contul to tonster
A metusn shatement causef the prgom
back to the coller ot a mehod. A eturn pe mo7
erimiHue type like int Aloat dou ble o eterehce dyo
dy ee Caeturn nothiry)

The type et doto netusn ed by o method must be 2ompitable


dith tth netun tyee seeeiled by methd, Pos inttonee it
the metun ype ot Some nethod ethod is beoleon we not
den on

The Nomable eeejuly the Noue retumed by a meHhod must


also be competible with the metun type speci tied fr method,

mNt be
The Ramometer con be possed
aLLephed by he method i
n
sequente they
Some Seunze

Publie e lass A
int add co withut Arayumey

tnt 70

publie shadhe yoid main (Steiry Co )


ot add 2 al, addeo J

3
publie lass 8
int odd cint ,ty) cwith oyument
int

int add bl, add ( lo, no)he


Sy she moUt. pointlo/ gum ot t-Aacd).
* Constueto
et ieie init eJizadion
telnpurpose o Constrv
The maln
at tnsanee Ngmable
Aelogg nane nst be Sorme.
Pe poinh Constctor ngm
not
Cooghu ctoy s method ony s hieh daeg
vojd
ony retuTn type lndudiy
eoch objeet (onstr etor oi11 be ege cued
for

Con strctor ol| be erected outo matleady when


Wee meate on abjeet of clasg,

Sytaelass dlass Nome)

JassHorme inputamys)
/iniAalieadtto t tnstonce vonable

Notes! hone publie pohade pteeded detelt


doeoye consttor in jona
Tajono 2ontyerto <on not be a e abstsat
sdsno stoie.

automaticodly creote defeult contntn


(Dilemenee between constetor o)ethod
Constetors Medthodg
Canstru ztors
nust be some hiryos
Consluetors it not hoviry hethad is houing
hovig
oy vetusndyee jncdude oid. Retumotyee

tos eoch objeeh onatcto for aseh objeet metthod cwit1


ony nember o t e
RNecute ony

To usnite busjneAs loqie


nontate orñaloles,

when le create obiee

ClossName Ret. narsablen


ne ClussNarneo)
Usert-deAne Reterence
dodotyp yoriable, To create neu) objectin
Peteren ced T# oill hol etence hesup memor
datotyee oo Qmeate itonce yan'able

datatype ocddress bt on ohjee t addes ot an objeek


wuiny asiqn ment operdor
Class Nomec) thatmemer adoreuS
Tt is a constov cto will be sford inide
Use to nitalee instonee Nonalaen a sbje
Tiere ame 29ngtrem rohsfeno)
defeul t . o botba
parometemaad Snozdhe
* Use of this keytrd s eters
redeenee vdiable thad
Tn joNO this is a
Ho the (usentont objecd this is mern ber.
Teteon ce vaable to curent elass
objeet ?rtoce
mes
uSe dhis keyia d dny number ot

Use> To occeSS rend dass intonee mem borT,


ghor)
Tnstonce mem beng !. instonce vormables t nethods Cin
methd
Hote USe this key word omywh2re ecept otate
state bloek.

thìs ìs keywosd
this is
keyward uId t mete eurent cless in stonce
nAmbes Cinstonee Nojablay A metha
thiskeywocd we con use to ditterenti ate beteen loeal ond
qlobad vojable otz
we con vse this key Dord insid e d method jnide a
congtrute

sbclassTest doss yz
bertbam sbat publie stette yoid madnCtiyg)
Najd setyalue (intiD
this.i t setvelwe eio);
3 t. showC)
void shouw4
System.out pritn ()
(0
snstonee block
Sste m.ou.ptla (

pvblhe stdie Ned nalnc

Pinal Bemobl ock fe new Anem8/ock

stadie etho eneuo


mr method eyewiog
Snsteze method ee wton sol
Consta etu method eneutonmaols 22olb

Olocks

Trstonce block statie block.

Clacs Clpss Hem closs cJg1SAone


stodte &
I iost onee blo ck I)stedie bloek
ony
stadte yornable,

cehen we ereate objeet


closs betore con strueefo
code ineide
weWe con woite
me consumnig
Use ot blocks
block |ike JoB2c9nne
6lutStatle bloek
Tnstonee block
thatie
objeeh statie block deas
O Instonce blaek desl wth
| irstonce blo<k statie | state block

statie Nonabe

tiAt the tme lass fle


When we coede objet
class betore onstasbs loadin in sv
iostonee block wl RReAte sostade bJoek w

Instonce block eeess Stedie


va)bey. snly steie odebles.

eeeute dd the time a ob)eet RYeLute as he me


erreadton betore eonstuotor eecudio. (oading class le in
mmomy
* Inhegitance
Prpenteg
The poU ERSS G# when one elass oCquiri The

etie in ehild <Jagr.


prbcess 6t aquie parent lass propo

object con d yuies oJ ropetied


The
o porent eloss.

Synta Cextends

child-eloss lkeyuoordteo porent coss


Tt is implement
to eladfership.

Typs of Inhestonce!

O Single or sìmple
ultilevel
Hierorehiad
lHple
Hybrid
Gydie
d
Note- oTovo sug posts for sin mulhlRNel Het osehio
joheri tonce ela sses t jntentoeeg
by
Tavo Suppt tr mulple Hy brid nheritanee by
Snqle simple Tnheritone !

sìngle Po rent f single ehild


closs
yojd mOf
Sepln c porent eloss ethod"');

Class C extend p
Leolsbltrds
Sopln n ehild loss mathod')
me m

Pel ne

slemiz o slpal2

S sentenee Tohei tenee,


The Proces Where Joss ocqu)ne prvpesey

The ot acas poret loss openthes


The
prpershey methods porent eloss.
eronhie
iohertdonee by wsiry elasrf metheds.
Multi-l eNel shesitonee |

slngle po ert p single ehild but in multtple (eve

Closs PS
soplnc porent class p-) na method":2ols oog

3
Jass eeyotends P
bidsDnlgoz
Soplnehil eloss m2 method

bns9 oly
Qoss ce entend s <
Nojd
Sopln ( chìld clags c< p3 methad'

3
Jova sypport multple tybrnd
jnhertone by us iadertaee
Tohesitonee!

sìngle po ment mslple ehildg

Closs
Nold ml(§
sopln ( pore nt zloss p m methad'y

Clo ss
Noid mn c)
soplne" 2hìld zlass <m2 method)

U9st Ca ertendsP
SDpln ci' ehi ld Ches nethod'%
Anaedenence voriable
f tis a non Statie bomedin.
seper eloss (porent ) closs
F Supenr key wo-d holdsoe sont to reter sup er coss ngtoneeC
Aomn sub- eloss 14
*
keyedord
nem beS then use super instone
ditteretiade subdass nstence yoables s meta
To intone
yorlabey
methos from su perrduss Ynstonce

in stodie meBhodSm
cwe con not use Super keyuo
stetie aettho bloeks

this() superC), botfs


* this (3 superr <) both ane 2onstvato calls to ce
Super class eontrt (urneteloss constru torr batks

*thìsC) ¢ supers () betth we 9nUseSe in lonstuee


Dnl as

but not both simu ltanneoushy hos


ssilorhioi
publie elass AAA $ Publlz coss BB8 etends AA
dora yoid me

Syso(this.X)
sysol super),

Publ< sftie void madn (Sfiry I] or


poblie Jass CCC §

ong cc congtruete')
sysoZ" o
20

Ccccintx)
oy cee (onstvcto,
L Sy so " int
ste

publit loss DeD aghends Cc<


syper Cio);

new 00D()
emo Closs
D
Test mport
pI TeIt closs
Pe;Poekaye pl;pockage
Pe
pntecey
c/osses
metheds
Cojova
Com
methodg corejava, fockage!
(om,
nome. Pockage
Shatemenoekge
stat
omme, joyo yw^ nmet is
stete frogt statemente
package one most odo)te con only ckage po
jotesloce class eer me T0
otpost oîs fockagetime
jte closseg Teladed
unit sìrgle itoad soce
d ispactge
qoup thoehonis
fo umencopsuladin simple fockae'
ois K
tolder.
Tnport
Test Jovd sfatement
pockaye
1st statment
mpost sfadement
od statement
Closs eelosodion
jotetoce derleratiay or muliple na)n
o)|o
maùn

Soya
t AceJs madiAres in
tho powatenool

Proteeted
publie

proteeted publle dre the keyods.


# Note 0 Psate tust 2oncept. onA
keydord t s
detaw |t is oet
sibot
oheme to oeess' elass
Aecess nodifre aoil tel Us
Norjablest methods
’ t o set oeess perm)
ss)ons we e Use fecess modif
publie (Glabal level)
poekaye
Aecess wthln te package toutde the
leue
Pshate celoss lene)
Aeeess within the elasf only

Pro eehed!
Access - oithin tte poekage outeide ot poekaye 8nlg
n child elosseg uding chilo reterenceg.
Aecegs 9odiery

Closs level modiie menber eve modie

Vosiaubleyor mthods
Top leve edose Tnner leNe elog ostab

defaslt Jeteult OPrvate Pivete


Anal no brdetewlh DProteete
Oprotede

zborttsm25doio
dol
poe kaqe
Prcapsulatt.
9m.jbk. encap
puble clags Student

psluate shin name


pssat nt ollun
publiz Student(Shor ome dauble molks, nt nllrdon\
dhis, ndme nome
ttìs . osks modky' slduolo

publie Notd setNome(triry nomd}


this.ndme nameosiom

puble dodble gettnosky <) .


etusn mosk,
publiz void setosky CAoble mofk)s
at dhis monkese mosky

publiz iotqettollNun ()

publiz voie setkollNum ()


Ruble 2oss (madn

Student

S setnome ("Jag s0ga'),


S. sethoese g9.5)
S set RotNom ( in)

Sheiy nome: S.getrome(),


oloube modks sgeAM9desc))
ind ollNum, SigetRdl| Num)
Sy so(nome)

Fncapss ladieg bfor sildug


gouplry maehonlsn sheme gDup Voñabley
slngle unit called os clas
doss is o best Romple of enepsulatto9d

bioy ildug
metho
Ngojable
Hide doda torr seeuity pupa
too to achieve encap suladteg'

by na kr metods publieprNateu
Class tBkBork Dato hlding-
psivade long oLeountNumb em moky Narleble of
pivate double balonce
erivade
puslle void get&alonce C)
if lnakdatk user)
2/outhentteatea wse
sapleCbalone;
/| vnaudhetcat usersm
pleoge proujde yal)d usemome):
Soplo

G dato behidd nethod called os encopsulatioga


tiding
cass,mon roveh2n
Smo dspolt on
templade or blaepsint Aoom whìzh obje et one
0 closs îs_a
closs }s abjeeh which hone 9mm an prpertieymeh
<lass is used to daAine userdetned dadodadotyty pe,
pe,
o9.
eloss does not megure anemory ter eeCuti
<lass s a o loql<a entt
objet ill corta
elosr detesmines ho objeels kehose whieh
oheets gven in closr chìeh have Some pmpesy fm etha
A
il

We con not eoeade obieet ojtht elosseoh


Cbiect ie 2olleetton otdatomethod
(4 cb jeet is on th whieh hane sfotesbehone
pmony forms
poouiding mulHple mplementadn
The process t one bpernation bosed
to o method to peshorm
subtype or bosed on porometer tny pe is caJlo

mborhsm 9h92

Compile time
Cstadie polymasghis)
Metho hethod Closs Tet
Oyesloadiy
) Some Nome void shod (into)
( gome name
Some elosj dih dosf
Same om
No.ot yeld shon) (iot a, lnts)
sopln C"a);
p-sm
show (o
man method ? methodo in a clasj by
we ovesload medn
0 Con ot dwg,9 calls
hane Ory Tueq
ThÌs s Beeguse
We con
method overload'y
maln method Jhieh

o Me~hed Ouevidin doe

closs est
Testt ne) TestC);
yo'd shoul)
S0p";

Class xyz etend Test shaul


yoid shou
Sop('2");

(Oo Neroy onethod must haNe some ren


type n Subhpe
it is passi ble to hade
etosa ty pe ter a oNerdly me thod in
G

Child eloss but ehilcds Rturn dype sheld be sub


setuon type is alled conarjont re
* Mathod cuerloading' mItiple method
TA the lass (s hovlng
ment ist.
Some nome but ditferent orsg

* TMP poit. Two on is howly some ngme


nethods
m0nme methods
losr.
methods must be dedined o some
list.
methods wtth dihement ¡ny ment
>nvmber ot 290 be diAterent
’Seguenee of amg. lan be dreren 22olo
9n be difhencnt
buode biol
Note- Ouerloadad nethods mog have
have dhe
dhe Som
Some or dt etn pe

IA the ela ss is hav Ing muitiele (onseor with


Some nome bot di h ong
list

9
onstuehog mst be defne hoin Some name
n sdme lasg.

Constrcterg jth dif!. omg list.
number ong- yn be dtt
9n be Jitt
mnadhod
* Methed Overriding hoe the same ovenniidiry,
ehild class
colled method
-> Ip cogs
poet jt is
declored n the the ehild
method n
porent elass
dening
nee - Re. methad
Jassalled ag
poret
some
so Me nome os
o ln the Jore
tt
he
ho method mt hove
method ne posometert
the some
mehod must hone
fhe porent eloss
Is-4 melattonship
There must be hoNing
dw6 closses thet
metbod Nereiding eeeus n
posentehiid elactfonghp bodfam nohanE
elos chid closs medhod
Nate- 0 Both porent name.
haNe he ome method muSt hoye
zhild elass methodg
Both poest eloss
the gaMe aeturn ypes
dogs eijd eloss methodg must honve,
6 Both orent
the some ponomefen 1'st

9d ifo 0ouote

Ouedloadlrg Olerniding
js harg
honlng multiple ORe detniry the porenf doss
medheos th the some nome metho chio Jas.
but dt dmg. ist
Metho o ouerjdiy
method overtoading tos clossey thot hon porent
2h)/ latonship.
ovenrload)ng pa 6d mete
n method oveminy
6 In method poroome te s must game
|st must be dipteren+
netha oNRrmoiy s a ame
9D method overtoadiy is o eomple
ot campi1e me polymophlcm
method ovenidiy both
porent elass ehi eass methos
moy hoNe the same t n tye
must hawe some

A-21d t2u
ibtetho
Methad Oleooidlng hidiny
O Instonce nethod Oatadte method qlobe/ vor
tn method oNemallr n method hì d)ngmethad
medhodmesoion bosesresotuon
objeet
Qunttge pohmonghsm Gmp) le Hme polymenphism
bis0ho0
method nesoluHon oj! be ) method reso)o wll be
act copile 4tme oniboolas
closs
S polnt
ceated.
Juprint Arorn cokteh obfe ats ifmethod
demplte os 20mno
20mmo) paperhy
class is
hjeod dbieh hove
ruf od wbleh objeel wil!
onine how sbjeeh behove
eloss dete is used to delne clos
keylod ohich
closs
closs is logieal entiy
dedine wsetdadne datotyp,
de ss is Used to
eecio),
clocs doss not g ulre mermo for meheds block sh
e cen ereate
constueste
STnside o chss

object.
dbject is collecten ot dato ¢ methads
obje et is on ingtonce ot elogs
cbjeet is ed oosld entiy
behove
object is on entty cohieh hog stades
objects thaving guln memoy kog txecuton
eon not (oeate Okjeet withot lass
doss
e con coeate mony ob'e ett ot one
s closs by eatiry on objee.

eneopsulation
It qoup mo chonism where we con qroup Narìables
s
method toto slngle un)t ealled elass.
is bert eo mple ot encopsul ation
9Hiding dato behind ometh ods te ealled ehcapsulaton
the
we con oeheve encopsu(afio) Vonables osopNat
moklng
wzhiese encapsolaton by mokiy meth ods as opubliz.
6) Dato
hidny
iding meons id moktng af vohalbles pvate

decegs
furekoaiy o ob

You might also like