An abstract class can be considered as a blueprint for other classes, allows you to create a set of methods that must be created within any child classes built from your abstract class. A class which contains one or abstract methods is called an abstract class. An abstract method is a method that has declaration but not has any implementation. Abstract classes are not able to instantiated and it needs subclasses to provide implementations for those abstract methods which are defined in abstract classes. While we are designing large functional units we use an abstract class. When we want to provide a common implemented functionality for all implementations of a component, we use an abstract class. Abstract classes allow partially to implement classes when it completely implements all methods in a class, then it is called interface.
Why use Abstract Base Classes :
Abstract classes allow you to provide default functionality for the subclasses. Compared to interfaces abstract classes can have an implementation. 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 in an application, but can also help you when working on a large team or with a large code-base where keeping all classes in your head at the same time is difficult or not possible.
How Abstract Base classes work :
In python by default, it is not able to provide abstract classes, but python comes up with a module which provides the base for defining Abstract Base classes(ABC) and that module name is
ABC. ABC works by marking methods of the base class as abstract and then registering concrete classes as implementations of the abstract base. A method becomes an abstract by decorated it with a keyword
@abstractmethod. For Example –
I have 3 sides I have 4 sides I have 5 sides I have 6 sides
I can walk and run I can crawl I can bark I can roar
Implementation Through Subclassing :
By subclassing directly from the base, we can avoid the need to register the class explicitly. In this case, the Python class management is used to recognize
PluginImplementation as implementing the abstract
A side-effect of using direct subclassing is it is possible to find all of the implementations of your plugin by asking the base class for the list of known classes derived from it.
Concrete Methods in Abstract Base Classes :
Concrete classes contain only concrete (normal)methods whereas abstract class contains both concrete methods as well as abstract methods. Concrete class provide an implementation of abstract methods, the abstract base class can also provide an implementation by invoking the methods via
Let look over the example to invoke the method using super():
Abstract Base Class subclass
In the above program, we can invoke the methods in abstract classes by using
Abstract Properties :
Abstract classes includes attributes in addition to methods, you can require the attributes in concrete classes by defining them with @abstractproperty.
Can't instantiate abstract class parent with abstract methods geeks child class
In the above example, the Base class cannot be instantiated because it has only an abstract version of the property getter method.
Abstract Class Instantiation :
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.
Traceback (most recent call last): File "/home/ffe4267d930f204512b7f501bb1bc489.py", line 19, in c=Animal() TypeError: Can't instantiate abstract class Animal with abstract methods move
- C# | Abstract Classes
- Data Classes in Python | Set 4 (Inheritance)
- Data Classes in Python | An Introduction
- Data Classes in Python | Set 2 (Decorator Parameters)
- Data Classes in Python | Set 3 (dataclass fields)
- Data Classes in Python | Set 6 (interconversion to and from other datatypes)
- Data Classes in Python | Set 5 (post-init)
- Scala | Abstract Type members
- jQuery | Get and Set CSS Classes
- CSS | Pseudo-classes
- Generic Classes in Scala
- When to use static vs instantiated classes in PHP?
- Understanding Classes and Objects in Java
- jQuery | multiple classes Selector
- Use of :even and :odd pseudo-classes with list items in CSS
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.