Python Class|Object|Abstraction|and Inheritance With Example |
Python Class|Object|Abstraction|and Inheritance With Example
Class: A-Class is a collection of data members and member functions.
Data members are the variable used inside the class. Member functions are the function used inside the class.
Syntax of class in Python
Class Class_name:
Data member 1
Data member 2
Data Function 1
Data Function 2
class student:
Name="icoderweb"
Roll No=100
Object: A Object is having states and behaviors in which state means what does it has and behavior means what does it do.
Create the object of class: We can create the object of the class by using the class name. There is no need for a new keyword like java in Python to create objects.
Syntax of object
obj_name=class_name()
class student:
Name="icoderweb"
Roll No=100
s=student()
Here s is the object of a class student.
Accessing variable of class in Python
We can access the variables of the class by using the dot(.) operator.
How To Use variable class in Python?
class student:
name="icoderweb"
roll=2022
s=student()
print("Student Name:",s.name)
print("Student Rollno:",s.roll)
*****OUTPUT*****
Student Name: icoderweb
Student Rollno: 2022
How To Use class with variable and function?
class Student:
name="icoderweb"
roll=2022
marks=88.3
def showdata(self):
print("Student Name:", self.name)
print("Student Rollno:",self.roll)
print("Student Marks:",self.marks)
s=Student()
s.showdata()
*****OUTPUT*****
Student Name: icoderweb
Student Rollno: 2022
Student Marks: 88.3
Constructor: A constructor is a special member function of a class that executes when we create the object of that class.
In another word, we can say that there is no need to call a constructor.__init__() method is used as a constructor in Python. There are two types of constructors are used in Python.
*****OUTPUT*****
Student Id:807716
1.Parameterized Constructor
2.Non-parameterized Constructor
Parameterized Constructor: A parameter The constructor with a parameter inside the variable is called Parameterized Constructor.
print("Student Name:", name)
print("Student Rollno:",roll)
print("Programming Language:",lan)
s1=Student("icoderweb",2022,"Python")
*****OUTPUT*****
Student Roll no: 2022
Programming Language: Python
Non-parameterized Constructor: A constructor with no parameter Value is called a Non-parameterized Constructor.
print("Hello Student I am Non-Parameterized Constructor")
s=Student()
*****OUTPUT*****
Hello Student I am Non-Parameterized Constructor
Child/Sub/Derived class: A class that inherits the property of another class is called Child or Sub or Derived class.
def rectangle_area(self,h,w):
area=h*w
print("Area of Rectangle:",area)
class Square(Rectangle):
def square_area(self, side):
area = side*side
print("Area of Square:", area)
d=Square()
d.rectangle_area(20,20)
d.square_area(18)
*****OUTPUT*****
Area of Rectangle: 400
Area of Square: 324
Single Inheritance: In a single inheritance only two classes are used in which0 one is inherited by another.
def rectangle_area(self,h,w):
area=h*w
print("Area of Rectangle:",area)
class Square(Rectangle):
def square_area(self, side):
area = side*side
print("Area of Square:", area)
d=Square()
d.rectangle_area(20,20)
d.square_area(18)
*****OUTPUT*****
Area of Rectangle: 400
Area of Square: 324
In the above example, we can see that there are only two classes are used in which Rectangle is inherited by Square.
Therefore using the object of Square we can call the function rectangle_area() and square_area().
Multiple Inheritance: When two or more than two classes are inherited by a single class simultaneously called multiple inheritances.
area=h*w
print("Area of Rectangle:",area)
class Square:
def square_area(self, side):
area = side*side
print("Area of Square:", area)
class Triangle(Rectangle, Square):
def triangle_area(self, length,breadth):
area = 0.5*length*breadth
print("Area of Triangle:", area)
obj=Triangle()
obj.rectangle_area(20,20)
obj.square_area(18)
obj.triangle_area(25,25)
*****OUTPUT*****
Area of Rectangle: 400
Area of Square: 324
Area of Triangle:312.5
Here Rectangle and Square are Base classes and Triangle is derived class so we can call all the functions using the object of Triangle class.
Multilevel Inheritance: When the first class is inherited by the second class, the second class is inherited by the third class, and so on is called multilevel inheritance.
In this type of inheritance, each derived class is the base class for the next class. In this type of inheritance, at least three classes are compulsory.
area=h*w
print("Area of Rectangle:",area)
class Square(Rectangle):
def square_area(self, side):
area = side*side
print("Area of Square:", area)
class Triangle(Square):
def triangle_area(self, length,breadth):
area = 0.5*length*breadth
print("Area of Triangle:", area)
obj=Triangle()
obj.rectangle_area(20,20)
obj.square_area(18)
obj.triangle_area(25,25)
*****OUTPUT*****
Area of Rectangle: 400
Area of Square: 324
Area of Triangle:312.5
Hierarchical Inheritance: When a single class is inherited by two or more than two classes simultaneously called hierarchical inheritance.
In another word, we can say that in this type of inheritance derived class maybe two or more than two but the Base class should be one.
area=h*w
print("Area of Rectangle:",area)
class Square(Rectangle):
def square_area(self, side):
area = side*side
print("Area of Square:", area)
class Triangle(Rectangle):
def triangle_area(self, length,breadth):
area = 0.5*length*breadth
print("Area of Triangle:", area)
obj=Triangle()
obj.rectangle_area(20,20)
obj.triangle_area(25,25)
*****OUTPUT*****
Area of Rectangle: 400
Area of Triangle: 312.5
In the above example, you can see that there are three classes(Rectangle, Square, and Triangle) that are used in which Rectangle is inherited by Square and Triangle.
def rectangle_area(self,height,width):
area=height*width
print("Area of Rectangle:",area)
class Square:
def square_area(self, side):
area = side*side
print("Area of Square:", area)
class Triangle(Rectangle, Square):
def triangle_area(self, length,breadth):
area = 0.5*length*breadth
print("Area of Triangle:", area)
class Circle(Triangle):
def circle_area(self, radius):
area = 3.14*radius*radius
print("Area of Circle:", area)
obj=Circle()
obj.rectangle_area(20,20)
obj.square_area(18)
obj.triangle_area(12,25)
obj.circle_area(5.6)
*****OUTPUT*****
Area of Rectangle: 400
Area of Square: 324
Area of Triangle: 150.0
Area of Circle: 98.4704
In the above example, you can see that there are four classes(Rectangle, Square, Triangle, and Circle).
How To Use Constructor in Python.
class student:
def__init__
(self,id,name,age,email,address):
self.id=id
self.name=name
self.age=age
self.email=email
self.address=address
def printdata(self):
return f"Student Id:{self.id}\n
Student Name:{self.name}\n
Student Age:{self.age}\n
Student Email:{self.email}\n
Student Address:{self.address}"
s=student(807716,"Python Programmer",25,"icoderweb@gmail.com","Noida")
print(s.printdata())
*****OUTPUT*****
Student Id:807716
Student Name:Python Programmer
Student Age:25
Student Email:icoderweb@gmail.com
Student Address:Noida
1.Parameterized Constructor
2.Non-parameterized Constructor
Parameterized Constructor: A parameter The constructor with a parameter inside the variable is called Parameterized Constructor.
How To Use Parameter Constructor in Python?
class Student:
def __init__(self,name,roll,lan):
print("Student Name:", name)
print("Student Rollno:",roll)
print("Programming Language:",lan)
s1=Student("icoderweb",2022,"Python")
*****OUTPUT*****
Student Name: icoderweb
Student Roll no: 2022
Programming Language: Python
Non-parameterized Constructor: A constructor with no parameter Value is called a Non-parameterized Constructor.
How To Use Nonparameter Constructor in Python?
class Student:
def __init__(self):
print("Hello Student I am Non-Parameterized Constructor")
s=Student()
*****OUTPUT*****
Hello Student I am Non-Parameterized Constructor
Inheritance: The process of getting the property of one class into another class is called Inheritance.
In another word, we can say that the process of deriving a new class from an old class is called inheritance.
When a class inherits the property of a class it means it can access all the data member and member functions of that class except the private element.
In this type of programming mainly two types of classes are used.
1.Parent/Super/Base class
2.Child/Sub/Derived class
Parent/Super/Base class: A class that is inherited by another class is called Parent or Super or Base class.
1.Parent/Super/Base class
2.Child/Sub/Derived class
Parent/Super/Base class: A class that is inherited by another class is called Parent or Super or Base class.
How To Use Inheritance in Python?
class Rectangle:
def rectangle_area(self,h,w):
area=h*w
print("Area of Rectangle:",area)
class Square(Rectangle):
def square_area(self, side):
area = side*side
print("Area of Square:", area)
d=Square()
d.rectangle_area(20,20)
d.square_area(18)
*****OUTPUT*****
Area of Rectangle: 400
Area of Square: 324
Single Inheritance: In a single inheritance only two classes are used in which0 one is inherited by another.
How To Use Single Inheritance in Python?
class Rectangle:
def rectangle_area(self,h,w):
area=h*w
print("Area of Rectangle:",area)
class Square(Rectangle):
def square_area(self, side):
area = side*side
print("Area of Square:", area)
d=Square()
d.rectangle_area(20,20)
d.square_area(18)
*****OUTPUT*****
Area of Rectangle: 400
Area of Square: 324
In the above example, we can see that there are only two classes are used in which Rectangle is inherited by Square.
Therefore using the object of Square we can call the function rectangle_area() and square_area().
Multiple Inheritance: When two or more than two classes are inherited by a single class simultaneously called multiple inheritances.
In another word, we can say that in this type of inheritance Base class maybe two or more than two but the derived class should be one.
In this type of inheritance, at least three classes are compulsory.
How To Use Multiple Inheritance in Python?
class Rectangle:
def rectangle_area(self,h,w):
def rectangle_area(self,h,w):
area=h*w
print("Area of Rectangle:",area)
class Square:
def square_area(self, side):
area = side*side
print("Area of Square:", area)
class Triangle(Rectangle, Square):
def triangle_area(self, length,breadth):
area = 0.5*length*breadth
print("Area of Triangle:", area)
obj=Triangle()
obj.rectangle_area(20,20)
obj.square_area(18)
obj.triangle_area(25,25)
*****OUTPUT*****
Area of Rectangle: 400
Area of Square: 324
Area of Triangle:312.5
Here Rectangle and Square are Base classes and Triangle is derived class so we can call all the functions using the object of Triangle class.
Multilevel Inheritance: When the first class is inherited by the second class, the second class is inherited by the third class, and so on is called multilevel inheritance.
How To Use Multilevel Inheritance in Python?.
class Rectangle:
def rectangle_area(self,h,w):
def rectangle_area(self,h,w):
area=h*w
print("Area of Rectangle:",area)
class Square(Rectangle):
def square_area(self, side):
area = side*side
print("Area of Square:", area)
class Triangle(Square):
def triangle_area(self, length,breadth):
area = 0.5*length*breadth
print("Area of Triangle:", area)
obj=Triangle()
obj.rectangle_area(20,20)
obj.square_area(18)
obj.triangle_area(25,25)
*****OUTPUT*****
Area of Rectangle: 400
Area of Square: 324
Area of Triangle:312.5
Hierarchical Inheritance: When a single class is inherited by two or more than two classes simultaneously called hierarchical inheritance.
In another word, we can say that in this type of inheritance derived class maybe two or more than two but the Base class should be one.
In this type of inheritance, at least three classes are compulsory.
How To Use Hierarchical Inheritance in Python?.
class Rectangle:
def rectangle_area(self,h,w):
area=h*w
print("Area of Rectangle:",area)
class Square(Rectangle):
def square_area(self, side):
area = side*side
print("Area of Square:", area)
class Triangle(Rectangle):
def triangle_area(self, length,breadth):
area = 0.5*length*breadth
print("Area of Triangle:", area)
obj=Triangle()
obj.rectangle_area(20,20)
obj.triangle_area(25,25)
*****OUTPUT*****
Area of Rectangle: 400
Area of Triangle: 312.5
In the above example, you can see that there are three classes(Rectangle, Square, and Triangle) that are used in which Rectangle is inherited by Square and Triangle.
Therefore using the object of Triangle we can call the function only rectangle_area() and triangle_area()
because there is no relation between Square and Triangle.
Therefore function square_area() can not be called by the object of Triangle. Similarly, by using the object of class Square we can call only the function rectangle_area() and square_area().
Hybrid Inheritance: The combination of two or more than two inheritance is called Hybrid inheritance.
Hybrid Inheritance: The combination of two or more than two inheritance is called Hybrid inheritance.
It can be a combination of any two or more than two inheritance(single, multiple, multilevel, hierarchical).
In this type of inheritance, at least three classes are compulsory.
In this type of inheritance, at least three classes are compulsory.
How To Use Hybrid Inheritance in Python?.
class Rectangle:
def rectangle_area(self,height,width):
area=height*width
print("Area of Rectangle:",area)
class Square:
def square_area(self, side):
area = side*side
print("Area of Square:", area)
class Triangle(Rectangle, Square):
def triangle_area(self, length,breadth):
area = 0.5*length*breadth
print("Area of Triangle:", area)
class Circle(Triangle):
def circle_area(self, radius):
area = 3.14*radius*radius
print("Area of Circle:", area)
obj=Circle()
obj.rectangle_area(20,20)
obj.square_area(18)
obj.triangle_area(12,25)
obj.circle_area(5.6)
*****OUTPUT*****
Area of Rectangle: 400
Area of Square: 324
Area of Triangle: 150.0
Area of Circle: 98.4704
In the above example, you can see that there are four classes(Rectangle, Square, Triangle, and Circle).
which Rectangle and Square are inherited by Triangle class so in class Rectangle, Square and Triangle.
1. Code Reusability: It means the function inside the base class is shared by all the derived classes.
2. Time-Saving: Because there is no need to define the existing property(same code) of a class in another class.
3. It helps to reduce code redundancy.
Data Abstraction: Abstraction means Data hiding, in another word we can say that in this type of programming essential data is shown to the user or outside class and unessential data is hidden.
In Python if we want to perform data hiding then it can be done by using double underscore(__) prefix with variables or functions then they can not be accessed outside that function.
There are Multiple inheritances but the class Triangle is inherited by Circle so in class Triangle and Circle there is Single inheritance.
Therefore the above program is a combination of Multiple and Single
inheritance so it is called Hybrid Inheritance.
Advantage of Inheritance
1. Code Reusability: It means the function inside the base class is shared by all the derived classes.
2. Time-Saving: Because there is no need to define the existing property(same code) of a class in another class.
3. It helps to reduce code redundancy.
Data Abstraction: Abstraction means Data hiding, in another word we can say that in this type of programming essential data is shown to the user or outside class and unessential data is hidden.
In Python if we want to perform data hiding then it can be done by using double underscore(__) prefix with variables or functions then they can not be accessed outside that function.