Chapter-2 Inheritance
Chapter-2 Inheritance
title
Chapter-2 Inheritance
class Teacher(Person):
def __init__(self,name,age,exp,r_area):
Person.__init__(self,name,age)
self.exp = exp
self.r_area = r_area
def display_data(self):
Person.display(self)
print("Experience:",self.exp)
print("Research Area:",self.r_area)
def calculate_interest(self):
interest = self.balance * self.interest_rate
self.deposit(interest)
print("Interest added. New balance:", self.balance)
class CurrentAccount(Account):
def __init__(self, account_number, balance, overdraft_limit):
Account.__init__(self,account_number, balance)
self.overdraft_limit = overdraft_limit
# Example usage:
savings_acc = SavingsAccount("SA123", 1000, 0.05)
savings_acc.deposit(500)
savings_acc.calculate_interest()
Multiple Inheritance
In [2]: class Base1:
def method1(self):
print("Method 1 from Base1")
class Base2:
def method2(self):
print("Method 2 from Base2")
obj = MyClass()
obj.method1() # Output: Method 1 from Base1
obj.method2() # Output: Method 2 from Base2
Method 1 from Base1
Method 2 from Base2
In [1]: class A:
def method(self):
print("A method")
class B(A):
def method(self):
print("B method")
class C(A):
def method(self):
print("C method")
In this example, the MRO of class D will be [D, B, C, A, object]. When you
call D().method(), Python will first look for the method() implementation in
class D, then in B, then in C, then in A, and finally in the base class object.
This is the order in which the methods will be resolved and executed.
TYPES
1.Multi-level inheritance
2. Multipath Inheritance
Deriving a class from other derived classes from same base class is called
multipath inheritance
Example-1 Write a program that has as abstract class polygon. Derive two
classes Rectangle and Triangle from polygon and write methods to get
their details of dimensions and hence calculate area.
class Rectangle(Polygon):
def get_data(self): # Method Overriding
self.length = int(input('Enter length:'))
self.breadth = int(input('Enter breadth:'))
def area(self):# Method Overriding
self.area = self.length*self.breadth
return self.area
class Triangle(Polygon):
def get_data(self):
self.base = int(input('Enter base:'))
self.height = int(input('Enter height:'))
def area(self):
return 0.5 * self.base *self.height
r1 = Rectangle()
r1.get_data()
print("Area:",r1.area())
t1 = Triangle()
t1.get_data()
print("Area:",t1.area())
p1 = Polygon()
p1.get_data()
p1.area()
Area: 6
Area: 1.0
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
Cell In[9], line 31
28 print("Area:",t1.area())
30 p1 = Polygon()
---> 31 p1.get_data()
32 p1.area()
NotImplementedError:
class SavingsAccount(Account):
def __init__(self, account_number, balance, interest_rate):
super().__init__(account_number, balance)
self.interest_rate = interest_rate
def calculate_interest(self):
interest = self.balance * self.interest_rate
self.deposit(interest)
print("Interest added. New balance:", self.balance)
class CurrentAccount(Account):
def __init__(self, account_number, balance, overdraft_limit):
super().__init__(account_number, balance)
self.overdraft_limit = overdraft_limit
In [ ]: