0% found this document useful (0 votes)
12 views11 pages

OOPS in Python (Inheritance, Polymorphism, Exception Handling)

object oriented python

Uploaded by

mdzayed2003786
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)
12 views11 pages

OOPS in Python (Inheritance, Polymorphism, Exception Handling)

object oriented python

Uploaded by

mdzayed2003786
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/ 11

#Program add two no's using class and object

class add:

x = 459

y = 989

def sum (self):

print("Addition =",self.x + self.y)

obj = add()

obj.sum()

output:
Addition = 1448

#Sigle inheritance
class parent:

def display(self):

print("PARENT CLASS")

class child(parent):

def show (self):

print("CHILD CLASS")

c = child()

c.display()

c.show()

output:
PARENT CLASS
CHILD CLASS

#Multilevel inheritance
class grandparent:

def display(self):

print("Grand parent class")


class parent (grandparent):

def show(self):

print("Parent class")

class child(parent):

def cdisplay(self):

print("Child class")

c = child()

c.display()

c.show()

c.cdisplay()

output:
Grand parent class
Parent class
Child class

#Multiple inheritance
class father :

def fdisplay(self):

print("FATHER CLASS")

class mother :

def mdisplay(self):

print("MOTHER CLASS")

class child(father,mother):

def cdisplay(self):

print("CHILD CLASS")

c1=child()

c1.cdisplay()

c1.mdisplay()

c1.fdisplay()

output:
CHILD CLASS
MOTHER CLASS
FATHER CLASS

#Hierarchical inheritance
class parent:

def display(self):

print("PARENT CLASS")

class child1(parent):

def c1display(self):

print("CHILD1 CLASS")

class child2(parent):

def c2display(self):

print("CHILD2 CLASS")

c1 = child1()

c2 = child2()

c1.c1display()

c1.display()

c2.c2display()

c2.display()

output:
CHILD1 CLASS
PARENT CLASS
CHILD2 CLASS
PARENT CLASS

# hybrid inheritance
class School:

def func1(self):

print("This function is in school.")

class Student1(School):
def func2(self):

print("This function is in student 1. ")

class S

tudent2(School):

def func3(self):

print("This function is in student 2.")

class Student3(Student1, School):

def func4(self):

print("This function is in student 3.")

# Driver's code

object = Student3()

object.func1()

object.func2()

output:
This function is in school.
This function is in student 1.

#Cyclic inheritance
class A(A):

def method1(self):

print("A class method1")

a = A()

output: #(A) base class and A drived class so showing error (Ex) how is it possiable we born our self

error

#Polymorphism
#1)Operator overloading

a=("ganesh")
b=("rani")

c=a+b

print(c)

a=10

b=20

c=a+b

print(c)

output:
ganeshrani
30

#2)Method overload
class MOverload:

def display (self,a = None,b = None):

print(a,b)

obj = MOverload()

obj.display()

obj.display(10)

obj.display(10,20)

output:
None None
10 None
10 20

#3)Methor overriding
class Father:

def Transportatation(self):

print("CYCLE")

class Son (Father):

def Transportation(self):
print("BIKE")

obj = Son()

obj.Transportation()

output:
BIKE

#Encapsulation 1
class robo:

__x = 100 #private

def __chitti(self):

print(self.__x)

obj = robo()

obj.chitti() #error

print(obj.__x)#error

#Encapsulation 2
class robo:

x = 100 #public

def chitti(self):

print("Hai")

obj = robo()

obj.chitti()

print(obj.x)

#Abstract method1
from abc import ABC, abstractmethod
class AbstractDemo(ABC):

@abstractmethod

def display(self):

None

class Demo(AbstractDemo):

def display(self):

print("Abstract Method")

obj = Demo()

obj.display()

output:
Abstract Method

#ABC 2

from abc import ABC,abstractmethod

class AbstractDemo(ABC):

@abstractmethod

def display(self):

None

@abstractmethod

def show(self):

None

class Demo(AbstractDemo):

def show(self):

print("Show method")

def display(self):

print("display method")
obj = Demo()

obj.display()

obj.show()

output:
display method
Show method

#interface1
from abc import ABC, abstractmethod

class Vehicle(ABC):

@abstractmethod

def no_of_seats(self):

pass

@abstractmethod

def break_type(self):

pass

v=Vehicle()#in interface not accessing object

v.no_of_seats()

v.break_type()

output:

error

#Interface 2
from abc import ABC, abstractmethod
class Vehicle(ABC):

@abstractmethod

def no_of_seats(self):

pass

@abstractmethod

def break_type(self):

pass

class Car(Vehicle):

def no_of_seats(self):

print("4 seats")

def break_type(self):

print("special as well as powerful break")

class Bike(Vehicle):

def no_of_seats(self):

print("2 Seats")

def break_type(self):

print("Normal break")

c = Bike()

c.no_of_seats()

c.break_type()

output:
2 Seats
Normal break
#Exception handling

Exception error
#syntax error
for i in range(10)
print(i)

#logical error
a,b = 10,20
c = a-b
print(c)

#Runtime error
a = int(input())
b = int(input())
print(a/b)
output:
20
0
ZeroDivisionError: division by zero
#Exception handling program
a = int(input("Enter value for a:"))
b = int(input("Enter value for b:"))
try:
c = a/b
print(c)
except:
print("Exception Raised")
else:
print("No Exception......")
finally:
print("Program Ends.....")

output:
Enter value for a:1000
Enter value for b:0
Exception Raised
Program Ends.....

You might also like