Sie sind auf Seite 1von 4

OOPS Concept With Real Life Example

OOPS Concept With Real Life Example


This is the most asked Question in a technical interview in any domain.. OOPs Concept is very
very important.. Today I will explain OOPs concept with real Life Example that will help you to
rasp the concept well and excel in the interviews...
Objects: O!"ect is the !asic unit of o!"ect#oriented prorammin.O!"ects are identified !y
its uni$ue name. %n o!"ect represents a particular instance of a class. There can !e more than one
instance of an o!"ect. Each instance of an o!"ect can hold its own relevant data.
%n O!"ect is a collection of data mem!ers and associated mem!er functions also known as
methods.
Classes: Classes are data types !ased on which o!"ects are created.O!"ects with similar
properties and methods are rouped toether to form a Class. Thus a Class represent a set of
individual o!"ects. Characteristics of an o!"ect are represented in a class as Properties. The
actions that can !e performed !y o!"ects !ecomes functions of the class and is referred to as
&ethods.
Example #1:
'or example consider we have a Class of Cars under which (antro )in* %lto and +aan,
represents individual O!"ects.In this context each Car O!"ect will have its own* &odel*-ear of
&anufacture* Colour* Top (peed* Enine Power etc.*which form Properties of the Car class and
the associated actions i.e.* o!"ect functions like (tart* &ove* (top form the &ethods of Car
Class..o memory is allocated when a class is created. &emory is
allocated only when an o!"ect is created* i.e.* when an instance of a class is created.
Example #2:
%n architect will have the !lueprints for a house....those !lueprints will !e plans that explain
exactly what properties the house will have and how they are all layed out. /owever it is "ust the
!lueprint* you can0t live in it. 1uilders will look at the !lueprints and use those !lueprints to
make a physical house. They can use the same !lueprint to make as many houses as they
want....each house will have the same layout and properties. Each house can accommodate it0s
own families...so one house miht have the (miths live in it* one house miht have the 2ones live
in it.
The bluepint is the class!!!the house is the object! The people li"in# in the house ae $ata
stoe$ in the object%s popeties!
&bstaction: %!straction means showin essential features and hidin non#essential
features to the user.
'o E#! (ahoo )ail!!!
+hen you provide the user name and password and click on su!mit !utton..It will show
Compose*In!ox*Out!ox*(entmails...so and so when you click on compose it will open...!ut user
doesn0t
know what are the actions performed internally....It "ust Opens....that is essential3 4ser doesn0t
know internal actions ...that is non#essential thins...
'o E#! T" Remote!!
,emote is a interface !etween user and tv..riht. which has !uttons like 5 to 65 *on 7of etc !ut we
dont know circuits inside remote...4ser does not need to know..2ust he is usin essential thin
that is remote.

Encapsulation: Encapsulation means which !inds the data and code 8or9 writin
operations and methods in sinle unit 8class9.

'or Example:
% car is havin multiple parts..like steerin*wheels*enine...etc..which !inds toether to form a
sinle o!"ect that is car. (o* /ere multiple parts of cars encapsulates itself toether to form a
sinle o!"ect that is Car.
In real time we are usin Encapsulation for security purpose...
Encapsulation * &bstaction + ,ata -i$in#!
.nheitance: ;erivin a new class from the existin class*is called Inheritance.
;erived8su! class9 class is ettin all the features from Existin 8super class<!ase class9 class and
also incorporatin some new features to the su! class.
'or E.*
class %ddress
=
(trin name3
(rtin /.no3
(trin (treet name3
>
class Latest%ddress extends %ddress
=
(trin City3
(trin (tate3
(trin Country3
>
pu!lic class ?ishal
=
=
Latest%ddress la @ new Latest%ddress893
77%ssin varia!le accordinly...
>
>
In the a!ove Example class Latest%ddress ettin all features from the %ddress class.
In the Latest%ddress class we have total A properties..B are inherited from %ddress class and B
properties are
incorporated. (o In the class ?ishal we are declarin the o!"ect of class Latest%ddress and then
assin new varia!les usin the properties of the previous !ase classes... (o this is a nice example
of inheritance..
Pol/mophism :
Polymorphism means a!ility to take more than one form that an operation can exhi!it different
!ehavior at different instance depend upon the data passed in the operation.
6C+e !ehave differently in front of elders* and friends. % sinle person is !ehavin differently at
different time.
DC % software enineer can perform different task at different instance of time dependin on the
task assined to him ./e can done codin * testin * analysis and desinin dependin on the
task assin and the re$uirement.
BC Consider the stadium of common wealth ames. (inle stadium !ut it perform multiple task
like swimmin* lawn tennis etc.
EC If a irl is married and mother of D children doin teachin "o! then she is a women first **
teacher in a school when she is in school**wife of someone at home** mother of her children** and
o!vious dauhter of someone F may !e irl friend of someone 8"ust kiddin9 means a woman
plays diffent roles at different times dats the polymorphism 8many forms9.
Summa/:
OOPs have followin features:
6. O!"ect # Instance of Class
D. Class # 1lue print of O!"ect
B. Encapsulation # Protectin our ;ata
E. Polymorphism # ;ifferent !ehaviors at different instances
G. %!straction # /idin our irrelevant ;ata
A. Inheritence # One property of o!"ect is ac$uirin to another property of o!"ect

Das könnte Ihnen auch gefallen