What is meant by abstraction in python?Give suitable example.
What is meant by abstraction in python?Give suitable example.
620
28-Mar-2023
Aryan Kumar
19-Apr-2023Abstraction in Python is the process of hiding the real implementation of an application from the user and emphasizing only on usage of it. For example, consider you have bought a new electronic gadget.
In Python, abstraction is implemented using classes and objects, where we define abstract classes and methods that can be implemented in concrete subclasses.
Abstraction in Python helps to hide the complexity of a system and allows us to work with high-level concepts instead of dealing with low-level details. This can make code easier to understand, maintain, and extend.
The Animal class provides an abstraction of the concept of an animal that can speak, without specifying how each animal speaks. This allows us to work with high-level concepts like animals and their ability to speak, without worrying about the specific implementation details of each animal's speech.
Krishnapriya Rajeev
31-Mar-2023Abstraction is used to hide the internal functioning of a method from the user. They only interact with the external implementation of the function, but the internal working is hidden.
In Python, abstraction is used to hide data that is irrelevant to the user to reduce its complexity. We can accomplish this by using abstract classes.
An abstract class consists of one or more abstract methods (methods that do not contain their implementation). An abstract class can be inherited by the subclass and the abstract method is then defined in this subclass. However, we cannot create objects for the abstract class, ie. it cannot be instantiated.
Python provides the abc module to implement abstraction.
Example:
Python does not provide the abstract class itself, unlike other high-level languages, due to which we need to import the abc module that provides the base for defining Abstract Base Classes (ABC). ABC decorates the methods of the base class as abstract. We use the abstractmethod decorator to define an abstract method (or it automatically becomes the abstract method if no definition is provided).
Example: