Launched in 1991, Python is a user-friendly, high-level, general-purpose, interpreted language. It is used in back-end and software development, writing system scripts, and data science. Python is available for Windows, Linux/UNIX, macOS and others.
In this article, we will understand:
Also read: How to reverse a list in Python?
What is a Class?
Python is an object-oriented language. Objects are chunks of data and attributes and added functions in the case of Python.
Python classes are used to construct the object. They also hold the attributes of an object and the methods that can be used to change the state of an object. They are also called an ‘Object Constructor’ for simplicity.
A class in Python can be created using the keyword class.
class Biodata: def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender x = Biodata("Kyra", 32, "Female") print(x.name) print(x.age) print(x.gender)
The keyword class creates a new class called Biodata in our case. The function def __init__() is used to define the variables within the class, which hold the attributes of the object. ‘X’ is an object defined within the class. x.name, x.age and x.gender invoke the attributes related to the object x.
What is an Abstract Class?
An Abstract class is a collection of abstract methods, which have only definition and no implementation. An Abstract class is like a format for all other classes. A child class of the abstract class can also call and use the abstract methods — termed as an inheritance. It is used mostly for designing larger classes.
Abstract classes can be used to define APIs and a group of sub-classes that is very useful while implementing plug-ins. Python provides no in-built abstract classes, but they can be implemented using a built-in module called Abstract Base Classes (ABC).
Abstract Base Classes (ABC)
Abstract Base Classes in Python are used to define interfaces and help introduce virtual subclasses. ABC generally marks methods of the base class as abstract, using the @abstractmethod.
To define an Abstract Base Class in Python, the @abstractmethod keyword is used before the class, which is meant to be the abstract class.
from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def sides(self): pass class Triangle(Shape): # Overriding abstract method def sides(self): print("A Triangle has 3 sides") Driver Code R = Triangle() R.sides()
First, the abstract class is imported into the code using from abc import ABC.
The abstract class can also be implemented into the code using from abc import ABC – as seen here.
Then a base class is defined in which the abstract methods are defined in Python. These methods use the @abstractmethod keyword in the definition. Then their methods are inherited and overwritten by other classes to use them accordingly.
Also read: How to find the length of a list in Python?
Concrete classes with Abstract methods in Python
Concrete classes in Python can hold normal or concrete methods, while abstract classes can contain abstract and concrete methods.
The concrete class can provide the implementation of abstract methods, though. The implementation can be done using the super() function in Python.
import abc from abc import ABCMeta, abstractmethod class Hello(ABC): def world(self): print("This is the Abstract Base Class") class K(Hello): def world(self): super().world() print("This is the Subclass ") # Driver code r = K() r.world()
These are the simplest ways for using the abstract classes in Pythons.
Importance of Abstract Classes
- It makes managing larger codes easy, where remembering multiple different class names can get difficult.
- Convenience for an external application providing plugins as a common API is defined for the subclasses.
- It provides a default functionality for the base class.
You can download Python for supported OS here.
Also read: How to use the Python range() function?
A creative nerd, TT player, an avid reader, and an engineering student.