0% found this document useful (0 votes)
23 views14 pages

Unit 6 Oop Concepts and Syntax

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)
23 views14 pages

Unit 6 Oop Concepts and Syntax

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
You are on page 1/ 14

Programming and Problem Solving

UNIT: VI Object Oriented Programming

OOP Features Programming/Implementation


Features of OOP
▪ Class
▪ Object
▪ Methods
▪ Inheritance
▪ Polymorphism
▪ Data Encapsulation and Data Hiding
Class
▪ Not possible to built real world data using Built in Data type
▪ OOP Specifically designed to solve real world problems
▪ A Class is used to describe something real world entity
▪ Class provides a template or Blueprint
▪ We can create multiple objects for the same class
▪ Therefore Class is also called collection of objects.
▪ Class is user-defined data type.
▪ Example : Student Class
Class Example
Name of
class student():
Class

Keyword a=100
Shared Data by
class
b=200 class and objects

S = student() Creating
Object
print(student.a)
print(S.a) Access to Class Data
Outside class
print(student.b) by using Object and Class
Name.
print(S.b)
Objects
▪ Object is an instance of class
▪ According to previous example, all students of class are object of that
class.
▪ Hence class can have multiple instances(objects)
▪ Every object has some data and functions associated with it.
▪ These functions stores data in variables and respond to message which it
receives from other objects.
▪ Class is logical structure where object is physical actuality.
Object Example
class student(): Name of
Class
Keyword a=100
class Class and
b=200 Object Data

def display( ): Class


Function
print(“Class Function”)
def show(self): Object
Function
print(“Object Function”)
Creating
S = student() Object

student.display() Calling Class function


using class Name
S.show() Calling object Function
using Object
Methods
▪ Function associated with class
▪ Defines operations
▪ Only methods of class can access data of class
▪ Every object can have different data associated with it.
▪ Python OOP contains three types of methods
Constructor method
Object Methods
Class Methods
Methods Example
class student(): Name of
Class
Keyword def __init__(self,a,b): Constructor
class Function/Method
self.a=a
self.b=b
Class
def display( ): Function

print(“Class Method / Function”)


Calling Class Object
Method def show(self): Function

print(“Object Function”)
S = student(10,10) Object Creation and Calling Constructor
Method
student.display()
Calling object Function
using Object
S.show()
Inheritance
▪ In inheritance we create new class from existing class
▪ New class called Child/Subclass/Derived class Parent Class
Name, Roll_No
▪ This new class inherits attributes of parent class Add(), Display()

▪ Inheritance relation is also called “is a” relation


▪ Main advantage of inheritance is code reusability

Child Class
Example
class parent():
a=100
b=200 class child(parent):
a=100
b=200
class child(parent): def display(self):
def display(self):
print(self.a) print(self.a)
print(self.b)
print(self.b)
Polymorphism
▪ One Name many forms
▪ Different meaning to method in different context
▪ Example
Operator Overloading
3 + 5 --------> Answer is 8
“3” + “5” ----> Answer is 35
Polymorphism
class addition():
def addition(self, a, b=100):
self.a = a We called same function
self.b = b two times. Every time count
of argument is different.
if self.b is not None: Addition is calculated on the
print(self.a + self.b) basis of count of argument.
else:
print(self.a + self.a)
Calling Function addition
A = addition() with One argument
A.addition(10)
Calling Function addition
A. addition(10,100) with Two argument
Data Abstraction and Encapsulation
▪ In Data Abstraction only essential details are shown and implementation
details are hidden.
▪ Example: Television , DVD Player
▪ In Data Encapsulation class data is packed together and hidden from
outside access. It can be done by defining data private, public and
protected.
Data Abstraction and Encapsulation
class hiding():
Public Variable
a = 100
Protected
_b = 200 Variable
Private
__c = 300 Variable

def _display(self):
Protected
Method print(“I am protected function”)
Private
def __output(self): Method

print(“I am private Function”)

You might also like