OOPS in Python (Inheritance, Polymorphism, Exception Handling)
OOPS in Python (Inheritance, Polymorphism, Exception Handling)
class add:
x = 459
y = 989
obj = add()
obj.sum()
output:
Addition = 1448
#Sigle inheritance
class parent:
def display(self):
print("PARENT CLASS")
class child(parent):
print("CHILD CLASS")
c = child()
c.display()
c.show()
output:
PARENT CLASS
CHILD CLASS
#Multilevel inheritance
class grandparent:
def display(self):
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):
class Student1(School):
def func2(self):
class S
tudent2(School):
def func3(self):
def func4(self):
# 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):
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:
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")
def Transportation(self):
print("BIKE")
obj = Son()
obj.Transportation()
output:
BIKE
#Encapsulation 1
class robo:
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
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.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):
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.....