0% found this document useful (0 votes)
2 views11 pages

Encapsulation & Inheritance

Uploaded by

ektabhardwaj789
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)
2 views11 pages

Encapsulation & Inheritance

Uploaded by

ektabhardwaj789
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

neapsulation' Methods Variables) class

En dapsulatim dlechanisd wrapeing the data (Variaba)


and MeHhocds together linqe unt.
htdon
In Encapswlaion he Vartables oh a clacs wth e
Hhroug Method
6her classcs, and Can be accassed ondy

* A class i an Zxample o Cncapsulatim be CAWse


data abere and ethods toto &inde un?t.
bbicete internad Y2presentation
En capsvalion, Kide
outside . This s Caled Iotorasahion hiding
Data hdina Msing acKs ModifErsi
data anbers ard
* ncapsalation Can be aclive d by declañng the
a class either as private or Pso heched.
Wabiods
dhere is Mo direct Modifera
Private and Prote cted.
’ Pubdic
’ Private
Modifens.
Protecte d
This Can be achievzd by wsing Lingle unders core
double underscore& .
epresents the Protzched, and te doue uoderit ove
* inge undergcore
repasknts he Privae enmbe rd .

detint(s,naMe, Salary): accaSible withia


outside
Mame name > Pul: a class J
Croleded accessible wihia the clas
and itt sub-classes

Salarg PiatelaceKsitk ny wihin


a class |
* Pulic Acless Modifar
or cutsiclo

cass.
clas Enplogee:
df fait(Self, name Salan):
na Me naMl

Seif- Salag Salar Menbers

(accestng Pudli)
data )
| Pint ("'Name; ", [Link], 'galarg:, elf. solarg)

ALcesing2| iot `Name' emp. naMe


Salary,2mp. Solary)
eme. displag C) |(ealing Publie ahial)
%eclas)
olP » Name
NaMe
Lalary : ho000

the Pirate accesstbe ondg he


clast.
To def're a fivate Member oy ember

name two underseorea -)


* for Eaomple'

-Salar
Coomple class Enployee
delnit (Seif. na nte, falary):
ell- name - name Jata yemloer)
Qalary I(Pivatz denber)
inctance othody).
Pivatc hembces)
an ac eKible) I| frint ("Name [Link], lalary', s{.Glary)
rm aelass)
Emp - Employee ('sess, hoo0 o)
[Link])
=) Name Sess Salary, o00o

’ Froteched AcceSS odifier


The FRotechad Member fAs acees&tble rsm instde tha cla
and tte Sub-class.
dafine a Protected eber by Prefxing the wlem ber nane
wia an under &lore.
-Points
-Prject

class ompany:

Infosys' I| (Prsteched 'Member


class Eneloyez ( lompangd:
deh intt(sel{, name).
Mame 2 Mane
inttCel7)
Compang

Piot C"Employee Name': , [Link])


(Acasiag Protvete4)
TNenber fo cild ) // rint ( Working on Roject:", olf. _toject)
elass
c- dispkyc)
Trint (Prqject Rdet I) Diect aceue Protechrd)
c. Rojact)
data, member.
ole:
Emploee Name Jess
Werning on Prjet : Infesys
Project : Infosya
datsg En copsalaion
an
objecte fan Unaythoried access
Frevents other clasns from using the Pivate Mamber &
deftned wih fo the class.
t Prevents acu tdentad daa Modffecation
Modtftuatim by wstog Ptrate and
Protetzd access Kewhs
* Hedes to Bohante Seusity by keeptrg de Code ogte kaje
trom &ehonal fnhert kanc&
* Bundltng data and Methiods a clasA Makes Code
More eadalble and mato tat nable

Ofrator orrlonding
¥ OPzrator Oloaltng Means aiving extended Meantng beyend
heir Predaftnod oprakooat Marng
for Baompe ,Operator u8 Rd to add two inbegers s

butltin bperator or unckion lhaws daferent


Called
bohavior ler ohjecte dfevent clatses
oferaer bvaleadig
Fxomple ofe
Priat (1oo + A0o) 300

t (oncatenate two ings


tat ('Pyion+ Proqrammi ng ) Pyhon Protramning
two list

Pint C[1o, 20, 30] +[bata s ructures, And, AlaoriA me J)


Oje - i0, 90, 30,'tata hrucurgs Aod Algortthma
Operator + is used to Carryout diferant Raratios
for dist inct data tyees
athon Provdes ome kaciot unckon
that is automatically nvoked whan is ass oci ated
Particul ar obiect.
used, the Maac Method add
automatically învoKed.
Eamele
Alass ?hem

dof Soit(Self, Price)


SoM-Pice : Price

Yetun Sel{. Price t other-Price


bl ?tem Choo)
b2item (30o)
, b+ b2)

Total Prie ;700


în [Link]
te Core Concepta in
Inheritan ce
Pregramntna (ooes) lorguages
a Ne chanism that allows ou to lreate
Aievarehy
Ne Ruocha b
classes Hhat shoare let d Properties and
a

Qng her class.


data a class ros an
class that nherthe all
Ioheritanca allous u8 to detne
a

uoperties rag ano ker elass


ther
he ethocls and
Hhe the closs beiog Rokertted from, also
Parent olass ’
called base closs

class is te class hat inkerihs rom anotker


chihd
clas
class also Called olated
Suntas
clads Baseclass
{Body3
class Dairedclacs ( Baseclasi:
iBody't.
Thare are
{ive tups inhetante anaidable , the

io Nuiple iaherttance
(iiy MalHevel foharitance
ci) Hierarchical nhettance
(V) IHabrtd inhertance
(

chitd alass ohertta from a

* In Sinale ioherttance
&togle - arent cla&k. and one pate nt clas
child clasa
Here is
Parent clas

chld clads

class Vektcde".
def Vektcetofo(sel4):
Print ('3ide Vehtele class')
class car(vektele): /(Ckild clas

Print Lnside Car class)

Car : arc) /Create objeet ot Car


[Link] _info () accRss Vehiclei tnto
Car. Car-infoc) Ws tng, car,obfect

Instde vehiele elass


Inaide Car class.

ciis Mul:*ple tnheritanle


* In mutiple.îherttance, one chtd Favent clas
class Can faher?t fron multipe panzat
classes kita clas
chtld class and
moliple Pareat clasaes.
Buam ple'
1)
class Fodem:/asnt class
Person?nfolseM, name, age )
int (Name;, name. 'Age' age ) I(Iside ers on
eluss )
class Company
Ae4 Compang-igo(salt, Company-name, Xocai on)
Priot ( Name Compar name, Locatians, Aocato0)
|Coropan
(clase
(ehild
(chia | clas Enplayee ¬Pevson, Company):
det Employeetofo (seg, salary, setll):
Print soalary, calary, 'Sutll:,suu) (Ingide Eneloye)
Telass
df Employee
(accass) I| emp. Per Sonînto Csessiea', 28)
(data emp. Company tofo (` Solosy, chenat)
emp. Emplo yee- toqo ( is00o, 'clod Conpuking )

Name Jessica Ag 28
Name Infobs locaten
ocation : Chen nai

Soakary: I6D00 gull, eloud Computi ng

ci Matevel înheritance
y n Mul4iheved nherftante, chass iokauts from Parznt c la6s

a child elass or dertved closs.


classes A,B,c |child e ass 1
t4 for sampe, we Cao 4aue three
A is the luporcla4 B is tte child class ot A
cild clas, 2
c is e chitd class o B.
’ chata sf clo0ses s Called Mulkilevel inherihaOcR
This a imlar do a chind and
Aavdfathe r.
Examgle'
class vehtele

Priat(nstde Vehtcde clas')


class Car Cvehicle):

frint ( Ingtode Cor class)


class &portsCarl Car):
def &ports ~Car-?nto Self): yclas 2
Print C'Instde Posts Car closi )
Kports Carc) /rate objec
perte car)
S_Car. Vehicde. intoc)
acless Vehices and Car, îot0
S_ Car. Carinfoc)
S~Car Spertar into C)

Inside vehicle clas


Lnside Car class
îoside SportsÉar elos

æ) 4ie rar chica herttance .


Pavznt class
* Jn Hirarchical
More thdn
is durived ron a lingde chitd clasi 1) |child clas1 child clasz)

Parznt Clos
* In o her isor da,
one Parznt class and multiple ehid elasses
when than dertred class are
Cheated ron
8ngle bos e class cale d hirardical ioeri tance
3
Excampe'
alass Wehicle |/Farent)
(class

class Car (ekitedec):


det Car- înfo ( Selk, name):
Priat Car name i8: rame)

class Truck ( vehicle)


):
dah ErucK_ Pntol self, name class2
Print (Truck name R6:, name)
class Bus(vektcle )
(chila)
dif Bustrfo(62lf, name):
Print Bs name iai . name)

Obj1 = Carc) for Car


Creat Dlajec srehicie)
alas data
obji. Car-intolBMw )
Craat objcct for TrucK
[Link]) rehi ce
(acles data from
obi2. brucK -into ( Fora )
Obj3 Bus ()
Cheate objcc fo
obja.îofol)
hic
objs. bus indo ( Benz ) acKsS data fren

Car name i6BMW


Vehide
Trucw name is ford
This is Vehicde
Bug na me i8 Benz
36)

(v) Hubd Inkorit ance Parant class

when înha ance Consiats


child clo1 child clo% 2
multiple hyees

Called hqbrtd tnhathance . child cioas 3

ot muiavel er?tante
Con bines multiele înherttantes
Nore Parent clasees, but ond
d clas lan fave
ave tuwo
tham ave dertved classes.
lxample
elass Vzhicde Parent
def Vehicletofo (self): elacs
Pint ("lossde Vehiele class')
class Car (Vahicle): (zhild.
def Car-into (sadf): yelass 1)
Printcnside Car class')
class Truck(vehiele): (chila
yelas 2
Priat(nside Trucn clas)
class Sports Car ( Car, vehicde); cid'clay
Car
def ports -Cat-înqo (se4):
class)
PrintCngide Sports Car
obgc for)
&portsCarc ) /Cate
8_Car.ehtele tofol) accuss data rr)
vehicle t Car)
S- tar. postBCar_rtol)

Inatde NYhicle claks


Snside Car clacs
Inside Sports Car class

You might also like