Python Inheritance Types

In Object-Oriented-Program An inheritance is consisting of classes and module or method.  A new class get access module, method, behavior and object from the inheritance. In python inheritance have two main class 1st is supper class and 2nd is sub class.

Python Inheritance Types

  1. Supper Class:

A supper class is a primary class and sub classes are child class. We clear inheritances in python with a simple example suppose you are developing a module about Student Enrollment System. So you need to create some classes in your project as below.

 

  1. Sub Class :

The sub class is member of supper class, on top of which it can add its own member. In sub class member may be the variables. The other sub classes are inherits from the supper class, in other world when you crate supper class and sub class so it’s become relationship between supper class and sub classes.

 

  1. Studetn => supper class
  2. Personal_info => sub class
  3. Group => sub class
  4. Course => sub class

In above example we create four classes the first is supper class and other three are the sub classes. Other three sub classes have relation with supper class like student.

Python 3.8

# supper class or parent class
class student:
group = None
course = None

def set_values(self, group, course):
self.__group = group
self.__course = course

def get_group(self):
return self.__group

def get_course(self):
return self.__course

#child class
class sadaqat(student):
def fname(self):
return self.get_group(), self.get_course()

#child class
class sharafat(student):
def fname(self):
return self.get_group(), self.get_course()


fname = sadaqat()
lname = sharafat()
fname.set_values('A1', 'ARTS')
lname.set_values('A2', 'SCIENCE')
print(fname.fname())
print(lname.fname())

In above example we write three classes 1st is student as supper class and other two student name as sub class or parent classes of student class. And we use the two method to access the main class member variables 1st is set method and 2nd is get method.

Example 02 . Python Inheritance Types:

 we create another example of python inheritance types with three shapes polygon, rectangular and triangle.

  1. Polygon => Supper Class
  2. Rectangular => Sub Class
  3. Triangle => Sub Class

her we create two member variables which common in above three classes.

  1. Width
  2. Height
Python 3.8 


# supper class or parent class

class polygon:
width = None
height = None

def set_values(self, width, height):
self.__width = width
self.__height = height

def get_width(self):
return self.__width

def get_height(self):
return self.__height


class rectangular(polygon):
def area(self):
return self.get_width() * self.get_height()


class triangle(polygon):
def area(self):
return self.get_width() * self.get_height() / 2


rect = rectangular()
tri = triangle()
rect.set_values(50, 40)
tri.set_values(50, 40)
print('Rectangular Area Is :',rect.area())
print('Triangle Area Is :',tri.area())


Output Is :
Rectangular Area Is : 2000
Triangle Area Is : 1000.0
Process finished with exit code 0

 

python inheritance types

Multiple Inheritance In Python :

When a sub class or child class derived from more than one supper class this types of inheritance is called multiple inheritance. In multiple inheritance all the features of supper class are derived into to sub classes.

Python Inheritance Exercises

python 3.8
python inheritance types

class Purchase_order:
order_no = ""

def mother(self):
print(self.order_no)


# Base class2
class Supplier:
suppler_name = ""

def supplier(self):
print(self.suppler_name)


# Derived class
class Product(Purchase_order, Supplier):
def order_info(self):
print("Order No. :", self.order_no)
print("Supplier Name :", self.suppler_name)


# Driver's code
p = Product()
p.order_no = "0001"
p.suppler_name = "abc"
p.order_info()

 

This Post Has One Comment

Comments are closed.