4. UNIT 6 OOP Inheritance
4. UNIT 6 OOP Inheritance
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