0 ratings 0% found this document useful (0 votes) 5 views 8 pages 4 Object Oriented Programming in Python
The document provides an overview of object-oriented programming concepts in Python, including class creation, object instantiation, constructors, and data hiding. It discusses key principles such as polymorphism, method overloading, method overriding, and inheritance (single, multiple, and multilevel). Additionally, it emphasizes the importance of encapsulation and abstraction in managing data within 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 or read online on Scribd
Carousel Previous Carousel Next
Save 4 Object Oriented Programming in Python For Later Unit:T¥. - Object Otferted Programmleag 4o
Pinan
Creating Class 3—
A clags in Python 13a user —deFined template for
creating objects .
When toe create aneo clas3 ,we define a ned type
of object.
We can then create routliple instances of this object
8
| ture
Tq lelass awe created using class Rael
5. Attributes ove Vatables olefined Inside --the- class
anol represent -the properties of the class,
1 ear
mple$-
ae define aiclasa' :
class Dogt
Sound = "back "| =H class attvibutes.
2 | Creating Object 3—
lL An object ig an instance of a class + |
2.| "Er represents a Specific implementeton. of the class
and holds ts on data,
+ Promple t=
Class Dog +
Sound = "back"
ct create an oblect from -the Class
dog |. = Doge)
HF Acces the claes athibute
print (dogt . Sound ) ; =
© scanned with OKEN Scanner2onstuctors ame spech
a) Methods usec te Hoty
Object. . : Re
. Drample os : 5
atk Non’ + Parameterized) Constructor i qos |
Class fytamples +
def _intt_ (self)! de No erquments
Self. message = Hello, World |!
def display Coelf)! :
puot Cee .message)
objt = Examples) , rs
| OBL. display c) eo : an aed
FEParameterixed Constuctor
Class Exampleo '
det init — (seit value): si Accept s anqumerds
Saf value= value U
der display Coeir):
prict Cf" Value! Feet. Va) veg")
Obj. = Brormple 2 (42) j 70 vane
obj2.. display C_) :
© scanned with OKEN Scanner| 2 Set parame
+ |The 9elf ‘parameter tn python tsa reference ~fo the
Current instance - of 9 class.
.| Et used to access and modi.
and metheds,
| udhen a _ctass Is dlefined , the Hirst Pama mefes of
es Constructor and instance merods must be
self.
By the class's crthlbshes
[a3 | Polymorphigm + Method Overloading and Metro
pO
Overrieliin
1: [Polymorphism allows" methods +to behave: different
based on the object on input.
2:|Polynowphier voor derived) From Greeks the erm
[terelly means 'Imany foame',
43] etrod Overloading :— :
Fython doesnt Suppor metned overload ing alivecl but
You Can achieve 1} using default prrvametors.
~Cranple -
Class Coloulatow | La MIR Dt
def 4d Csefyaib, C =0)! sec fs optional
veturn a+b+e shin gout
CAC = Coladatord) + shied 4
Plat Ccale.add C1o,20)) 3 calls wrih 2 avqumerds
—_|puarc Cale -addClo, 26,20)) “cals wits Bapuments
——— ‘ az .
© scanned with OKEN Scanner293.2] Method overriding = ides'a Specific
_— hen’ @ subclass” Provi
y occurs wo T already define
i implementetion of a mebrnd f
_ HS parent cla 98- —
_— a Oxample
—— Class farwent! ae
deF greetCaelt)! -
Pile Cello fom Parent ")
Class Child CPorent) | i
def qreet(C elf)’ sfeoverriding
pare. print Clyelo from chi'id )
obj = childc) ‘ ae ae.
Obj » Pret C) z-calis the ovenidden pmotod tnvabild
1| Data hiding is con
cept Which underlines the hiding
— ©F data or infienation fiom the usen.,
— 2+! is one of Key aspects of Object ~ Ottented
— Programming Shresteqies ,
— 3. De Includes object details such 03 daty members,
a Intema) Wot,
= i
Class BankAccount i
i det —init_ Ceerg balance)’
= [Seth balonee e
s—bdance —balanee a-pilvede ath bus,
© scanned with OKEN Scannerdef deposit Cself, amount):
self. balance |. = ammount
clef wWhdra® Cself , amount)!
'Ramount x= Sef. balance
. ae, balance, -'=qrnount+ |
else:
Pilot Cl Enea Pfietenet balance")
der get. balance Coetf)! cao
‘ wet 2efs_—balance | 4 Hie
account = BankAccount Clooo)
account. deposit (500)
account ,w ithdravo (800)
punr('!Balance:" , account . get_balancel))
t
+ |Hides Implementation eros From the user _ans
provides only essentia} ifoomattion «
—
ae
—
—
[En Python ; We can achieve dota abstraction by using
Abstract class and abstract classes can be
using abe Cabetrect base Class) module and abstract
etiod of abc module.
ee | nce . a
“Inheritance allows a claes Cchild class)-to derive
LProperties and behaviours fom another class 3 Cpare
Class),
—
~45 |Thheritance | Siygle Inheritance , Multiple! Snherttance
—~
esis
© scanned with OKEN Scanner* Parent claggi— tt t is
21s the clase being. inhertted from; 4190 cal leg
base class.
e child class s— x aia +
Tes class that inherits: from ahother class
called derived class.
veils
Single Inheettance
Siggle Inheritance endbles a derived: class-fo
lohente Propesties fiom a‘ Slagle paver Close ,
[ae ag
I tod
tel. ' i
Fig: Single Inherfiance.
* Code er ; : ad
Class Animal * q va
‘deg: pod Cself) + 5 nahad
PHotC Animal makes @ sound) od
class eq (Animal)!
dlef SoundCseif) ) st oveniding
puleecipog baste")
alog = Daqt)- ‘ ved bat meee
dog: SoundC) ;
a
© scanned with OKEN ScannerMulttple Inheritance !—
aan en_a class can be derived, Bom more than one bas,
—Telass this -lype of inher: . . <
elas P Fance |'8 called multiple
ea inheritances . Very, 5 .
—~q. [an multiple loheritances, all +he-Featunes of the base
—Telasses are Inhertted into-the derived Claas.
— LA ] B
I 1
Ss are
— 1
-fig- Multiple Thheritance .
«code: >
Class Father: tone i
der Personal ity Cseif) > - 7
pHlor ("' Father is - discsiplined ")
Class Mother: |
def _KindnesoCself) ‘| |
pHot Mother ig: Kad"). |
Class Child (Father | Mother)!
—_| Pas 9
~—_| child = childe)
| child « personality)
Child kindness)
© scanned with OKEN Scanner~ 4.5.3 (Multilevel Snherttance :
: “Bhertance |
hild belahonships. o
mlar-to a eelationship
Onda grandfather,
Linremedicie cassl
Detived class
fig. Muttiiewel Tyhertrance «
| Code !
Class Grandparent ‘ ;
def legacy C seit) | :
print Legacy from Grond parent")
Class Porent( Grandparent) * sin) oogl
deF quidance(saf)! ;
+ Print (\Gruidonce Bim Pores")
Class Child C Parent) *
Pass
= child = child)
E [child legacy)
Ls Child. guidance ¢)
Es | Ss
© scanned with OKEN Scanner