Unit 6 Oop Concepts and Syntax
Unit 6 Oop Concepts and Syntax
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
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()
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