0% found this document useful (0 votes)
11 views7 pages

5 Thnunit

The document provides various examples of inheritance in Python, including single-level, multiple, and multilevel inheritance, along with constructor and destructor overloading. It also covers abstract methods, method overloading, method overriding, and accessing base class methods using the super method. Additionally, it demonstrates customization of classes through the __str__() and __repr__() methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

5 Thnunit

The document provides various examples of inheritance in Python, including single-level, multiple, and multilevel inheritance, along with constructor and destructor overloading. It also covers abstract methods, method overloading, method overriding, and accessing base class methods using the super method. Additionally, it demonstrates customization of classes through the __str__() and __repr__() methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Simgle-level inharitance:

class A:
def accName(self):
self.name = input("enter your name: ")

def accMarks(self):
self.marks = int(input("enter your markks: "))
class B(A):
def accRoll(self):
self.roll = int(input("enter your rollno: "))

def display(self):
print(f"Name is {self.name}")
print(f"RollNo is {self.roll}")
print(f"Marks {self.marks}")

obj = B()
i = 1
while(i<=3):
obj.accName()
obj.accRoll()
obj.accMarks()
obj.display()
i = i +1

Constructor and destructor overloading in inharitance

class A:
def __init__(self):
print("Base class constructor is called.")
def __del__(self):
print("Base class destructor is called.")

class B(A):
def __init__(self):
print("child class constructor is called.")
def __del__(self):
print("child class destructor is called.")

b = B()

Calling Base Class Constructor and Destructor from Derived class


class A:
def __init__(self):
print("Base class constructor is called.")
def __del__(self):
print("Base class destructor is called.")

class B(A):
def __init__(self):
A.__init__(self)
print("child class constructor is called.")
def __del__(self):
A.__del__(self)
print("child class destructor is called.")
b = B()

output:
Base class constructor is called.
child class constructor is called.
Base class destructor is called.
child class destructor is called.

Passing parameters to Base Class Constructor from Derived Class Constructor


class persoInfo:
def __init__(self, name, rollno):
self.Name = name
self.RollNo = rollno
def display(self):
print(f"Name is {self.Name}")
print(f"RollNo is {self.RollNo}")
class Marks(persoInfo):
def __init__(self, name, rollno, m):
persoInfo.__init__(self, name, rollno)
self.mark = m

def display_marks(self):
print(f"Marks {self.mark}")

M1 = Marks("nikhil", 10, 99)


M1.display()
M1.display_marks()

output:
Name is nikhil
RollNo is 10
Marks 99

#Example of multiple inharitance

class Name:
def __init__(self, n):
self.name = n
def dis_name(self):
print(f"Name is {self.name}")

class Rollno:
def __init__(self, r):
self.rollNo = r
def dis_rollno(self):
print(f"roll no is {self.rollNo}")

class marks(Name, Rollno):


def __init__(self, n, r):
Name.__init__(self, n)
Rollno.__init__(self, r)
self.__m1 = int(input("Enter marks: "))
def dis_marks(self):
print(f"marks of sub1: {self.__m1}")

object = marks("nikhil", 19)


object.dis_name()
object.dis_rollno()
object.dis_marks()

print(object._marks__m1) #tric to accept private method outside the class.

output:
Enter marks: 99
Name is nikhil
roll no is 19
marks of sub1: 99
99

#Example of multilevel inharitance

class A:
a1 = 10
def helloA(self):
print("Hello form class A")
class B(A):
b1 = 20
def helloB(self):
print("Hello form class B")
class C(B):
c1 = 30
def helloC(self):
print("Hello form class C")
print(self.a1+self.b1+self.c1)
return 0

object = C()
print(object.a1)
print(object.b1)
print(object.c1)
print(object.helloA())
object.helloB()
object.helloC()

output:
10
20
30
Hello form class A
None
Hello form class B
Hello form class C
60

#Example of multilevel inharitance

class A:
def __init__(self, name):
self.name = name

def dis_A(self):
print(f"name is {self.name}")

class B(A):
def __init__(self, name, rollno):
A.__init__(self, name)
self.rollno = rollno

def dis_B(self):
print(f"Roll no is {self.rollno}")

class C(B):
def __init__(self, name, rollno, marks):
B.__init__(self, name, rollno)
self.marks = marks

def dis_C(self):
print(f"marks of sub1 {self.marks}") #if overloading is happened the the
child method will get executed.

object = C("nikhil", 49, 99)


object.dis_A()
object.dis_B()
object.dis_C()

output:
name is nikhil
Roll no is 49
marks of sub1 99

#abstract method
from abc import ABC, abstractmethod
class polygon(ABC):
def display_A(self):
print("contrete class method")

@abstractmethod
def no_of_sides(self):
pass

class triangle(polygon):
def no_of_sides(self):
print("triangle has 3 sides")

class square(polygon):
def no_of_sides(self):
print("square has 4 sides")

class rectangle(polygon):
def no_of_sides(self):
print("rectangle has 4 sides")

t = triangle()
s = square()
r = rectangle()

t.display_A()
t.no_of_sides()
s.display_A()
s.no_of_sides()
r.display_A()
r.no_of_sides()

output:
contrete class method
triangle has 3 sides
contrete class method
square has 4 sides
contrete class method
rectangle has 4 sides

#abstract method 2
class polygon():
def display_A(self):
print("contrete class method")

def no_of_sides(self):
#no_of_sides() is a regular method, if it does not override the
abstractclassmethod
pass

class triangle(polygon):
def no_of_sides(self):
print("triangle has 3 sides")

class square(polygon):
def no_of_sides(self):
print("square has 4 sides")

t = triangle()
s = square()

t.display_A()
t.no_of_sides()
s.display_A()
s.no_of_sides()

output:
contrete class method
triangle has 3 sides
contrete class method
square has 4 sides

#method overloading
Python does not support method overloading. We may overload the methods but can
only use the latest defined method.

class Test:
def product(self,a, b):
self.a, self.b = a, b
self.p = self.a * self.b
print("Product of two numbers is ", self.p)

def product(self,a, b, c):


self.a, self.b, self.c = a, b, c
self.p = self.a * self.b* self.c
print("Product is ", self.p)

t= Test()
t.product(4, 5) # This line shows an error
t.product(4, 5, 5)

alternate of method overloading


class Test:
def product(self,a = 1, b = 1, c = 1): #alternate of method overloading(we can
give defualt argument/ *args)
self.a, self.b, self.c = a, b, c
self.p = self.a * self.b* self.c
print("Product is ", self.p)

t= Test()
t.product(4, 5)
t.product(4, 5, 5)

output:
Product is 20
Product is 100

#method overriding
class A():
def hello(self):
print("hello form class A")

class B(A):
def hello(self):
print("hello from class B")

b = B()
b.hello()

output:
hello from class B

#accessing the method of of base class using super method


class A():
def hello(self):
print("hello form class A")
class B(A):
def hello(self):
super(B, self).hello()
print("hello from class B")

b = B()
b.hello()

output:
hello form class A
hello from class B

#Customization via Inheritance Specializing Inherited Methods


# 1. __str__()

class A():
pass
class B(A):
def __str__(self):
return "hello from class B"

a = A()
b = B()

print(a) #__str__() is not defined for object a1 so repr is called


print(b) #__str__( ) is defined for object bl

2. __repr__()
class A:
pass
class B:
def __repr__(self):
# return {1:"one", 2:"two"}
return (1, 3)
a1=A()
b1=B()

print("object a1 ptints : ",a1)


print("object b1 Prints :",b1.__repr__())

output:
object a1 ptints : <__main__.A object at 0x000002345CA57230>
object b1 Prints : (1, 3)

You might also like