Data Abstraction in Python
Data Abstraction in Python:
Data Abstraction in Python is the process of hiding the real implementation of an application from the user and emphasizing only on usage of it.
Basically, Abstraction focuses on hiding the internal implementations of a process or method from the user. In this way, the user knows what he is doing but not how the work is being done.
Why Do We Need Abstraction?
- Through the process of abstraction in Python, a programmer can hide all the irrelevant data of an application to reduce complexity and increase efficiency.
Syntax:
from abc import ABC
Class ClassName(ABC):
- In Python abstraction can be achieved using abstract class and it’s methods.
Abstract Class in Python
- A class containing one or more abstract methods is called an abstract class.
- As a property, abstract classes can have any number of abstract methods coexisting with any number of other methods.
- Abstract methods do not contain any implementation. Instead, all the implementations can be defined in the methods of sub-classes that inherit the abstract class.
- We have already discussed the syntax of abstract class above, let’s understand this by taking examples.
Code #1:
Run
from abc import ABC class llgm(ABC): #abstract classdef calculate_area(self): #abstract methodpass pass class Square(llgm): length = 5 def Area(self): return self.length * self.length class Circle(llgm): radius =4 def Area(self): return 3.14 * self.radius * self.radius sq = Square() #object created for the class ‘Square’ cir = Circle() #object created for the class ‘Circle’ print("Area of a Square:", sq.Area()) #call to ‘calculate_area’ method defined inside the class ‘Square’ print("Area of a circle:", cir.Area())
Output:
Area of a Square: 25
Area of a circle: 50.24
- An abstract class can have both a normal method and an abstract method
- An abstract class cannot be instantiated, ( we cannot create objects for the abstract class).
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment