Write a program in Python to demonstrate Simple inheritance, Multiple inheritance
Create a class Employee with data members: name, department and salary. Create suitable methods for reading and printing employee information |
The mechanism of designing or constructing classes from other classes is called
inheritance.
#totalprogrammingcode
The new class is called derived class or child class & the class from which this
derived class has been inherited is the base class or parent class.
In inheritance, the child class acquires the properties and can access all the data
members and functions defined in the parent class. A child class can also provide
its specific implementation to the functions of the parent class.
Syntax:
class BaseClass1
#Body of base class
class DerivedClass(BaseClass1):
#body of derived - class
Example:
# Parent class created
class Parent:
parentname = ""
childname = ""
def show_parent(self):
print(self.parentname)
# Child class created inherits Parent class
class Child(Parent):
def show_child(self):
print(self.childname)
ch1 = Child() # Object of Child class
ch1.parentname = "Vijay" # Access Parent class attributes
ch1.childname = "Parth"
ch1.show_parent() # Access Parent class method
ch1.show_child() # Access Child class method
Python provides us the flexibility to inherit multiple base classes in the child class.
Multiple Inheritance means that you're inheriting the property of multiple classes
into one. In case you have two classes, say A and B, and you want to create a new
class which inherits the properties of both A and B.
So it just like a child inherits characteristics from both mother and father, in
Python, we can inherit multiple classes in a single child class.
Syntax:
Syntax:
class A:
Programming with Python (22616)
Maharashtra State Board of Technical Education 94
# variable of class A
# functions of class A
class B:
# variable of class A
# functions of class A
class C(A, B):
# class C inheriting property of both class A and B
# add more properties to class C
Example:
class Add:
def Addition(self,a,b):
return a+b;
class Mul:
def Multiplication(self,a,b):
return a*b;
class Derived(Add,Mul):
def Divide(self,a,b):
return a/b;
d = Derived()
print(d.Addition(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
Output:
30
200
0.5
Example
class EmployeeBase :
name = ""
department = ""
salary = ""
def read_info(self) :
print(Fore.GREEN)
self.name = input("Enter your name : ")
self.department = input("Enter your department : ")
self.salary = input("Enter your salary : ")
class EmployeeDerived(EmployeeBase) :
def print_info(self) :
print(Fore.GREEN + "\n\n******** Your Information *********")
print("\nName : " , self.name)
print("\nDepartment : " , self.department)
print("\nSalary : " , self.salary , "" + Style.RESET_ALL)
obj = EmployeeDerived()
obj.read_info()
obj.print_info()
2. Python program to read and print students information using two classes usingsimple inheritance
class StudentBase :
name = ""
rollNo = ""
lastSemPer= ""
def read_info(self) :
print(Fore.GREEN)
self.name = input("Enter your name : ")
self.rollNo = input("Enter your roll number : ")
self.lastSemPer = input("Enter your last semister percentage : ")
class StudentDerived(StudentBase) :
def print_info(self) :
print(Fore.GREEN + "\n\n******** Your Information *********")
print("\nName : " , self.name)
print("\nRoll Number : " , self.rollNo)
print("\nLast Semister Marks : " , self.lastSemPer, "" + Style.RESET_ALL)
obj = StudentDerived()
obj.read_info()
obj.print_info()
3. Write a Python program to implement multiple inheritance
class Python :
def python(self) :
print("Python")
class Java :
def java(self) :
print("Java")
class Hello(Python , Java) :
def hello(self) :
print("Hello " , end = "")
obj = Hello()
obj.hello()
obj.java()
obj.hello()
obj.python()