0% found this document useful (0 votes)
7 views

4. UNIT 6 OOP Inheritance

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)
7 views

4. UNIT 6 OOP Inheritance

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/ 8

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
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