You are on page 1of 29

Object

Oriented Programming
• Learning Objectives:
At the end of this chapter the students will be able to:
• Understand about Object Oriented Programming(OOP) classes and objects
• Know the concepts related to OOP
• Objects
• Classes
• Encapsulation
• Data Hiding
• Abstraction
• Polymorphism
• Inheritance
• Know about the advantages of OOP over earlier programming methodologies
• An object-oriented programming (OOP) is a
programming language model which is organized
around "objects" rather than "actions" and data
rather than logic.
• Before the introduction of the Object Oriented
Programming paradigm, a program was viewed as
a logical procedure that takes input data,
processes it, and produces output. But in case of
OOP a problem is viewed in terms of objects
rather than procedure for
• doing it.
what is an object?
• An object can be anything that we notice around us. It can be a
person (described by name, address, date of Birth etc, his typing
speed), a cup (described by size , color , price etc.) , a car (described
by model , color , engine etc., its mileage, speed ) and so on. In fact
it can be an identifiable entity.
• The whole idea behind an object oriented model is to make
programming closer to they real world thereby making it a very
natural way of programming.
• The core of pure object-oriented programming is to combine into a
single unit both data and functions or methods that operate on that
data.
• Simula was the first object-oriented programming language. Java,
Python, C++, Visual Basic, .NET and Ruby are the most popular OOP
languages today.
Basic concepts related to OOP
The basic concepts related to OOP are as
follows:
– Objects
– Classes
– Encapsulation
– Abstraction
– Data Hiding
– Polymorphism
– Inheritance
Object
• An object characterised by:

• Identity: This is the name that identifies an object. For example Or an i-phone is
a mobile phone that has been launched by Apple Inc.

• Properties: These are the features or attributes of the object. For example a A
mobile phone has model, color, price as its properties.

• Behaviour: The behaviour of an object signifies what all functions an object can
perform. A mobile phone can click and store photographs (behave like a camera).

• So an object clearly defines an entity in terms of its properties and behaviour.


• The data of a particular object can be accessed by functions associated with that
object only. The functions of one object cannot access the data of another
object.
Class
• A class is group of objects with same attributes
and common behaviors. It is basically a blueprint
to create objects.
• Both data and functions operating on the data
are bundled as a unit in a class for the same
category of objects.
• example -mobile phone.
• A Windows phone, Android phone and i-phone,
all fall into the category of mobile phones. All of
these are instances of a class, say Mobile_phone
and are called objects.
• A real instance of a class is called an object and
creating the new object is called instantiation.
• Objects can also have functionality by using
functions that belong to a class. Such functions
are called methods of the class.
• This terminology is important because it helps us
to differentiate between functions and variables
which are independent and those which belong
to a class or object. Collectively, the fields and
methods can be referred to as the attributes of
that class.
Block diagram Of class Mobile_phone
A class is defined before the creation of objects of its
type. The objects are then created as instances of this
class
• As you can add, modify and delete the attributes of a class
dynamically i.e. at run time, similarly, you can
• add methods dynamically to an object or class. Consider the
code given below:
• class Health_profile:
• ...
weight = 89
blood_group= 'B+'
def play():
print " Come on lets play“
H=Health_profile()
H.play=play()
• H.play()
Built in class Attribute
class Test:
' "A sample class to demonstrate built in attributes" '
rollno=1
marks=75
def __init__(self,rollno,marks):
self.rollno=rollno
self.marks=marks
def display(self):
print (" Roll No : " , self.rollno)
print ("Marks : " , self.marks)
Print( "Test.__doc__:" , Test.__doc__)
print ("Test.__name__:" , Test.__name__)
print ("Test.__module__:" , Test.__module__)
• Print( "Test.__bases__:" , Test.__bases__)
• Print( "Test.__dict__:" , Test.__dict_)
Data Hiding
• Data hiding is a means to procure encapsulation. Python doesn't
really enforce data-hiding in the true
• sense. The Python approach to getting encapsulation is by simply
not using "private" data members. As
• discussed before, if something is particularly "private" you can use
the double leading underscores to mark
• it as such, but this of course is nothing more than a marking.
Basically it just acts as a reminder that such
• data is intended to be used only within the class. Remember that
Python is a dynamic language and that
• you can add attributes or methods dynamically on an object. In the
example given below the attribute
• pulse is dynamically added to the class Health_profile through H1.
class Health_profile:
pass
>>H1 =Health_profile()
H1.pulse = 75
If you try to access this hidden attribute, then an error message will be displayed.

Consider the following example of class Health_profile:


class Health_profile:
Weight=89
Blood_group='B+'
__BP=None #private member
def __init__(self):
self.__BP = "Hidden attribute"
def display_BP(self):
print self.__BP
>>>H =Health_profile()
>>>H.__BP
Traceback (most recent call last):
File "<input>", line 1, in <module&gt;
AttributeError: Example instance has no attribute '__BP
Problem1
A. Create the class SOCIETY with following information:
society_name
house_no
no_of_members
flat
income
Methods
1.An __init__ method
to assign initial values of society_name as "Surya Apartments", flat as
"A Type", house_no as 20, no_of_members as 3, income as 25000.
2.Inputdata( ) –
to read data members(society,house_no,no_of_members&income)
and call
3.allocate_flat().
allocate_flat( ) - To allocate flat according to income
Income Flat
>=25000 A Type

>=20000 and <25000 B Type

<15000 C Type

4.Showdata( ) - to display the details of the entire class


Problem2
Define a class ITEMINFO in Python with the following description:
ICode (Item Code)
Item (Item Name)
Price (Price of each item)
Qty (quantity in stock)
Discount (Discount percentage on the item)
Netprice (Final Price)
Methods
A member function FindDisc( ) to calculate discount as per the following rule:
If Qty<=10 Discount is 0
If Qty (11 to 20) Discount is 15
If Qty>=20 Discount is 20
A constructor( __init__ method) to assign the value with 0 for ICode, Price, Qty, Netprice
and Discount
and null for Item respectively
A function Buy( ) to allow user to enter values
for ICode, Item, Price, Qty and call function
FindDisc( )
to calculate the discount and
Netprice(Price*Qty-Discount).
A Function ShowAll( ) to allow user to view the
content of all the data members.
Inheritance
• In object oriented programming, inheritance is a mechanism in
which a new class is derived from an already defined class. The
derived class is known as a subclass or a child class. The pre-
existing class is known as base class or a parent class or a super
• The major purpose of inheriting a base class into one or more
derived class is code reuse.
• The subclass inherits all the methods and properties of the super
class.
• The subclass can also create its own methods and replace methods
of the superclass.
• The process of replacing methods defined in super with new
methods with same name in the derived class is known as
overriding
Types of inheritance.

Inheritance can be categorized into five types:


• Single Inheritance
• Multilevel Inheritance
• Multiple Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
Single Inheritance

This is the simplest kind of inheritance. In this,


a subclass is derived from a single base class.
Multilevel Inheritance
In this type of inheritance, the derived class becomes the base of
another class
Multiple Inheritance:

• In this type of inheritance, the derived class


inherits from one or more base classes
Hierarchical Inheritance:

In this type of inheritance, the base class is


inherited by more than one class.
Hybrid Inheritance:

• This inheritance is a combination of multiple,


hierarchical and multilevel inheritance.
Example of single Inheritance

You might also like