Object Oriented Paradigm ( Java )
-
Upload
sivaramakrishna-markandeya-gupta -
Category
Documents
-
view
258 -
download
1
Transcript of Object Oriented Paradigm ( Java )
-
7/23/2019 Object Oriented Paradigm ( Java )
1/43
Object Oriented Paradigm
(Java)
S.S.R.K.M.GUPTA. M.Tech. (M.Phil.)
Aditya !llege !" #ngineering $Techn!l!gy
S%ram&alem'*+
-
7/23/2019 Object Oriented Paradigm ( Java )
2/43
S!"t,are cri-i- in /+0(U.S.A)
-
7/23/2019 Object Oriented Paradigm ( Java )
3/43
Relative !-t1#2!rt in di2erent &ha-e-
-
7/23/2019 Object Oriented Paradigm ( Java )
4/43
!-t !" Maintenance &ha-e
-
7/23/2019 Object Oriented Paradigm ( Java )
5/43
A 3!!4 !n Pr!ced%re !riented lang%age
-
7/23/2019 Object Oriented Paradigm ( Java )
6/43
-
7/23/2019 Object Oriented Paradigm ( Java )
7/43
5ra,bac4- !" POP lang%age-
6+ 7 !" &r!ject c!-t ha- t! %tili8ed in Maintenance&ha-e.
S!"t,are maintenance can be di9c%lt and timec!n-%ming.
:hen change- are made t! the main &r!ced%re (t!&);
th!-e change- can ca-cade t! the -%b &r!ced%re- !"main; and the -%b'-%b &r!ced%re- and -! !n; ,here thechange may im&act all &r!ced%re- in the &yramid.
5eb%gging i- very di9c%lt i" line- !" c!de cr!--e-
0;000; and &r!grammer l!!-e- hi- c!ntr!l !n c!de. Thi- a&&r!ach d!e- n!t m!del real ,!rld &r!blem- very
,ell.
Gl!bal data i- acce--ible by all "%ncti!n-; thi- lead- t!
b%g-.
-
7/23/2019 Object Oriented Paradigm ( Java )
8/43
-
7/23/2019 Object Oriented Paradigm ( Java )
9/43
/
Str%ct%red v- OO #
-
7/23/2019 Object Oriented Paradigm ( Java )
10/43
A '&r!gram %-ing Str%ct%re
-
7/23/2019 Object Oriented Paradigm ( Java )
11/43
>%ncti!n- are called thr!%gh !bject-
-
7/23/2019 Object Oriented Paradigm ( Java )
12/43
#ach !bject ha- it- !,n -tate%-ed by it- "%ncti!n-
-
7/23/2019 Object Oriented Paradigm ( Java )
13/43
What is an Object?
An !bject i- a -!"t,are b%ndle !" related variable-and meth!d-. S!"t,are !bject- are !"ten %-ed t!m!del real',!rld !bject- y!% ?nd in everyday li"e.
@i-%al re&re-entati!n !" a -!"t,are !bject A bicycle m!deled a- a
-!"t,are !bject
Object (Agent) in the ,!rld
-
7/23/2019 Object Oriented Paradigm ( Java )
14/43
t i- an entity,ith a ,ell'
de?nedb!%ndary andidentity that
enca&-%late--tate andbehavi!r.
Operations
Attributes
Object
-
7/23/2019 Object Oriented Paradigm ( Java )
15/43
t i- !ne !" the&!--ible c!nditi!n-that an !bject may
e
-
7/23/2019 Object Oriented Paradigm ( Java )
16/43
t determine-h!, an !bject
act- andreact-.
t i-
re&re-ented bythe !&erati!n-that the !bjectcan &er"!rm.
Joel Santos
Enrolls()
updateSquad()
ObjectB- =ehavi!r
-
7/23/2019 Object Oriented Paradigm ( Java )
17/43
Alth!%gh t,! !bject- may -hare
the -ame -tate (attrib%te- andrelati!n-hi&-); they are -e&arate;inde&endent !bject- ,ith their !,n
%niC%e identity.
AthleteID: 3556Name Joel SantosStatus:NEW
Squad:None
AthleteID: 3557Name: Arja SolamoStatus:NEW
Squad:None
ObjectB- dentity
-
7/23/2019 Object Oriented Paradigm ( Java )
18/43
n 'lang%age; data i- n!t -ec%redand ea-ily c!rr%&ted by !ther-
ac ! ec - a a cann! e
-
7/23/2019 Object Oriented Paradigm ( Java )
19/43
ac ! ec - a a cann! ec!rr%&ted
by !ther- in !ther cla--
-
7/23/2019 Object Oriented Paradigm ( Java )
20/43
>eat%re- !" Object !riented
&aradigm
-
7/23/2019 Object Oriented Paradigm ( Java )
21/43
A ,ay !" @ie,ing the :!rld
Agent- and !mm%nitie- Me--age- and Meth!d-
Re-&!n-ibilitie-
la--e- and n-tance-
la-- Eierarchie-'nheritance
Meth!d =inding; Overriding; and
#
-
7/23/2019 Object Oriented Paradigm ( Java )
22/43
FF
Sending >l!,er- t! a >riend
S%&&!-e (Ravi) ,i-h t! -end !,er- t! a"riend; (@en% ); ,h! live- in a city many
mile- a,ay.
:hat -h!%ld d!H
-
7/23/2019 Object Oriented Paradigm ( Java )
23/43
F
The c!mm%nity !" agent-hel&ing me
Venus Florist (Prasad)
Agent (Mahesh)
Me (Ravi)
Delivery Person
Friend (Venu)
Flower Arranger
Wholesaler
Grower
Gardener
-
7/23/2019 Object Oriented Paradigm ( Java )
24/43
F*
An Ob-ervati!n
An !bject'!riented &r!gram i--tr%ct%red a- a c!mm%nity !"interacting agent-; called !bject-. #ach !bject ha- a r!le t! &lay.
#ach !bject &r!vide- a -ervice; !r&er"!rm- an acti!n; that i- %-ed by !ther
member- !" the c!mm%nity.
-
7/23/2019 Object Oriented Paradigm ( Java )
25/43
F
Agent- and !mm%nitie- S!l%ti!nI >ind an a&&r!&riate agent; namely Mahe-h;
and &a-- t! him a me--agec!ntaining my reC%e-t t i- the re-&!n-ibility!" Mahe-h t! -ati-"y my reC%e-t
There i- -!me meth!d -!me alg!rithm !r -!me -et !"!&erati!n- %-ed t! -ati-"y my reC%e-t
Thi- in"!rmati!n; i.e.; detail-; i- %-%ally hidden"r!m myin-&ecti!n.
An !bject'!riented &r!gram i- -tr%ct%red a- ac!mm%nity !" interacting agent-; called !bject-
#ach !bject ha- a r!le t! &lay
#ach !bject &r!vide- a -ervice; !r &er"!rm- anacti!n; that i- %-ed by !ther member- !" thec!mm%nity
-
7/23/2019 Object Oriented Paradigm ( Java )
26/43
:hat i- a me--age in OOPH
A reC%e-t "!r an !bject t! &er"!rm !ne !" it-!&erati!n- (meth!d-)
:hat i- a Me--ageH S!"t,are !bject- interact and c!mm%nicate ,ith
each !ther %-ing me--age-.
The !bject t! ,hich the me--age i- addre--ed(!%r=icycle)
The name !" the meth!d t! &er"!rm (changeGear-)
Any &arameter- needed by the meth!d (l!,erGear)
-
7/23/2019 Object Oriented Paradigm ( Java )
27/43
Me--age- and Meth!d-
Acti!n- i- initiated in !bject'!riented &r!gramming bythe tran-mi--i!n !" a me--age t! an agent (an !bject)re-&!n-ible "!r the acti!n
The me--age enc!de- the reC%e-t "!r an acti!n
t i- acc!m&anied by any additi!nal in"!rmati!n(arg%ment-) needed t! carry !%t the reC%e-t
The receiver i- the !bject t! ,h!m the me--age i--ent
" the receiver acce&t- the me--age; it acce&t- the
re-&!n-ibility t! carry !%t the indicated acti!n n re-&!n-e t! a me--age; the receiver ,ill &er"!rm
-!me meth!d t! -ati-"y the reC%e-t
-
7/23/2019 Object Oriented Paradigm ( Java )
28/43
Re-&!n-ibilitie-
n !bject !riented &r!gramming acti!n- are de-cribed interm- !" re-&!n-ibilitie-.
A reC%e-t t! &er"!rm an acti!n den!te- t! &er"!rm a ta-4t! get de-ired re-%lt.
:hen a &r!blem i- eval%ated in term- !" re-&!n-ibilitie-;
the ab-tracti!n level ,ill be increa-ed. The !bject- th%- bec!me inde&endent "r!m each !ther
,hich ,ill hel& in -!lving c!m&le< &r!blem-.
An !bject ha- a c!llecti!n !" re-&!n-ibilitie- related ,ith
it ,hich termed a- L&r!t!c!lD. t i- im&!rtant that !bject- be all!,ed t! &er"!rm their
ta-4 h!,ever they -ee ?t; ,ith!%t %nnece--aryinteracti!n- !r inter"erence ,ith !ther !bject-
-
7/23/2019 Object Oriented Paradigm ( Java )
29/43
la--e- and n-tance- #very !bject i- in-tance!" a cla--. A cla--
gr!%&- -imilar !bject-.
The cla-- i- the re&!-it!ry "!r behavi!ra--!ciated ,ith an !bject.
The meth!d inv!4ed by an !bject in re-&!n-et! a me--age i- determined by the cla-- !"the receiver.
=ehavi!r i- a--!ciated ,ith cla--e-; n!t ,ith
individ%al in-tance-. All !bject- that arein-tance- !" a cla-- %-e the -ame meth!d inre-&!n-e t! -imilar me--age-.
A l hi h f i
-
7/23/2019 Object Oriented Paradigm ( Java )
30/43
0
A class hierarchy of variousmaterial objects
Material Obe!t
Ani"al
Ma""al
#u"an
$ho%&ee%er
Florist
Flora
Artist Dentist
Painter
'i Ada"
Dog
Flash
Platy%us
Phyl
Plant
Flower
arnation
$allys Flowers
More abstra!t !lasses are listed near the to% o* the treeMore s%e!i*i! !lasses and individuals are listed near the botto"+
-
7/23/2019 Object Oriented Paradigm ( Java )
31/43
la-- Eierarchie- la--e- are arranged in a treeli4e -tr%ct%re called a hierarchy
A cla-- may have -everal ance-t!r-; %& t! root class
:hen y!% de?ne a -%b cla--; y!% -&eci"y it- -%&ercla--
#very cla-- may have 8er! !r m!re -%bcla--e-
The &rinci&le that 4n!,ledge !" a m!re general categ!ry i-al-! a&&licable t! a m!re -&eci?c categ!ry i- calledinheritance.
The cla-- !lor"st,ill inherit attrib%te- !" the cla-- (!rcateg!ry) Shop#eeper.
la--e- can be !rgani8ed int! a hierarchical inheritance-tr%ct%re.
A child cla--(!r -%bcla--) ,ill inherit attrib%te- "r!m a&arent cla--higher in the hierarchy. An ab-tract &arent cla--i- a cla-- "!r ,hich there are n! direct in-tance- it i- %-ed
!nly t! create -%bcla--e-.
-
7/23/2019 Object Oriented Paradigm ( Java )
32/43
Inheritance
-
7/23/2019 Object Oriented Paradigm ( Java )
33/43
nheritance ' termin!l!gy
Inheritance is a fundamental Object Oriented concept that a newclass acquires the features from another existing class, whichreflects reusability. nheritance all!,- &r!grammer t! de?ne a general cla--
3ater y!% de?ne a m!re -&eci?c cla-- Add- ne, detail- t! general de?niti!n
Ne, cla-- inherit- all &r!&ertie- !" initial; general cla--
A class can be defined as a "subclass" of another class.
The -%bcla-- inherit- all data attrib%te- !" it- -%&ercla--
The -%bcla-- inherit- all meth!d- !" it- -%&ercla--
The -%bcla-- inherit- all a--!ciati!n- !" it- -%&ercla-- The subclass can:
Add ne, "%ncti!nality
U-e inherited "%ncti!nality
Override inherited "%ncti!nality
-
7/23/2019 Object Oriented Paradigm ( Java )
34/43
An e
-
7/23/2019 Object Oriented Paradigm ( Java )
35/43
Meth!d binding
=inding i- ,hat ha&&en- ,hen a meth!dinv!cati!n i- b!%nd t! an im&lementati!n nv!lve- l!!4%& !" the meth!d in the cla--; !r !ne
!r it- &arent-
=!th meth!d name- and &arameter- are chec4ed =inding can ha&&en at t,! di2erent time-
!m&ile time ' -tatic binding ( !r ) #arly binding
R%n time ' dynamic binding ( !r ) 3ate binding
Java al,ay- -%&&!rt- t! dynamicbinding by de"a%lt.
-
7/23/2019 Object Oriented Paradigm ( Java )
36/43
Meth!d =inding
Object- are %-ed t! call meth!d-. Method Bindingi- an !bject that can be %-ed t! call
an arbitrary &%blic meth!d; !n an in-tance that i-acC%ired by eval%ating the leading &!rti!n !" ameth!d binding e
-
7/23/2019 Object Oriented Paradigm ( Java )
37/43
Meth!d =inding ' #
-
7/23/2019 Object Oriented Paradigm ( Java )
38/43
Meth!d Overriding
-
7/23/2019 Object Oriented Paradigm ( Java )
39/43
Meth!d Overriding
. S%b cla-- can !verride the meth!d- de?ned by the -%&er cla--.
F. Overridden Meth!d- in the -%b cla--e- -h!%ld have -amename; -ame -ignat%re ; -ame ret%rn ty&e and may have eitherthe -ame !r higher -c!&e than -%&er cla-- meth!d.
. Java im&lement- R%n Time P!lym!r&hi-m1 5ynamic Meth!d
5i-&atch by Meth!d Overriding. 3ate =inding
*. all t! Overridden Meth!d- i- Re-!lved at R%n Time.
. all t! a !verridden meth!d i- n!t decided by the ty&e !"re"erence variable Rather by the ty&e !" the !bject ,here
re"erence variable i- &!inting.6. :hile Overriding a Meth!d; the -%b cla-- -h!%ld a--ign either
-ame !r higher acce-- level than -%&er cla-- meth!d.
-
7/23/2019 Object Oriented Paradigm ( Java )
40/43
#
-
7/23/2019 Object Oriented Paradigm ( Java )
41/43
#
-
7/23/2019 Object Oriented Paradigm ( Java )
42/43
#
-
7/23/2019 Object Oriented Paradigm ( Java )
43/43
Summary of Object OrientedProgramming
#verything i- an !bject
Object- &er"!rm c!m&%tati!n by ma4ingreC%e-t- !" each !ther thr!%gh the &a--ing !"me--age-
#very !bject ha- itB- !,n mem!ry; ,hichc!n-i-t- !" !ther !bject-.
#very !bject i- an in-tance !" a cla--. A cla--gr!%&- -imilar !bject-.
The cla-- i- the re&!-it!ry "!r behavi!ra--!ciated ,ith an !bject
la--e- are !rgani8ed int! -ingly'r!!ted tree-tr%ct%re; called an inheritance hierarchy.