48. Abstract classes in Python
An abstract class can be considered as a blueprint for other classes. It allows you to create a set of methods that must be created within any child classes built from the abstract class. A class which contains one or more abstract methods is called an abstract class. An abstract method is a method that has a declaration but does not have an implementation. While we are designing large functional units we use an abstract class. When we want to provide a common interface for different implementations of a component, we use an abstract class.
Why use Abstract Base Classes :
By defining an abstract base class, you can define a common Application Program Interface(API) for a set of subclasses. This capability is especially useful in situations where a third-party is going to provide implementations, such as with plugins, but can also help you when working in a large team or with a large code-base where keeping all classes in your mind is difficult or not possible.
By default, Python does not provide abstract classes. Python comes with a module which provides the base for defining Abstract Base classes(ABC) and that module name is ABC. ABC works by decorating methods of the base class as abstract and then registering concrete classes as implementations of the abstract base. A method becomes abstract when decorated with the keyword @abstractmethod. For Example
# Python program showing
# abstract base class work
from abc import ABC
class Polygon(ABC):
# abstract method
def noofsides(self):
pass
class Triangle(Polygon):
# overriding abstract method
def noofsides(self):
print("I have 3 sides")
class Pentagon(Polygon):
# overriding abstract method
def noofsides(self):
print("I have 5 sides")
class Hexagon(Polygon):
# overriding abstract method
def noofsides(self):
print("I have 6 sides")
class Quadrilateral(Polygon):
# overriding abstract method
def noofsides(self):
print("I have 4 sides")
# Driver code
R = Triangle()
R.noofsides()
K = Quadrilateral()
K.noofsides()
R = Pentagon()
R.noofsides()
K = Hexagon()
K.noofsides()
Output
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
Abstract classes are incomplete because they have methods which have no body. If python allows creating an object for abstract classes then using that object if anyone calls the abstract method, but there is no actual implementation to invoke. So we use an abstract class as a template and according to the need we extend it and build on it before we can use it. Due to the fact, an abstract class is not a concrete class, it cannot be instantiated. When we create an object for the abstract class it raises an error.
Example:
Create an Abstract Base Class called Shape that include abstract methods area() and circumference(). Then derive two classes Circle and Rectangle from the Shape class and implement the area() and circumference() methods . Write a Python program to implement above concept. ( university question)
from abc import ABC
class Shape(ABC):
# abstract method
def area(self):
pass
def circumference(self):
pass
class Circle(Shape):
def __init__(self):
self.r=float(input('Enter radius:'))
# overriding abstract method
def area(self):
print("Area of circle=",3.14*self.r*self.r)
def circumference(self):
print("Circumference of circle=",2*3.14*self.r)
class Rectangle(Shape):
def __init__(self):
self.l=float(input('Enter length:'))
self.w=float(input('Enter width:'))
def area(self):
print("Area of rectangle=",self.l*self.w)
def circumference(self):
print("Circumference of rectangle=",2*(self.l+self.w))
c=Circle()
c.area()
c.circumference()
r=Rectangle()
r.area()
r.circumference()
Comments
Post a Comment