Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Abstraction in Python

Abstraction in Python is a concept that hides the internal workings of functions, allowing users to interact with basic implementations without needing to understand the underlying complexity. It can be achieved through abstract classes and interfaces, which serve as blueprints for subclasses that implement specific functionalities. The abc module in Python provides the necessary tools to define abstract classes and methods, enhancing application efficiency and reducing complexity.

Uploaded by

reddisandeep92
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Abstraction in Python

Abstraction in Python is a concept that hides the internal workings of functions, allowing users to interact with basic implementations without needing to understand the underlying complexity. It can be achieved through abstract classes and interfaces, which serve as blueprints for subclasses that implement specific functionalities. The abc module in Python provides the necessary tools to define abstract classes and methods, enhancing application efficiency and reducing complexity.

Uploaded by

reddisandeep92
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Abstraction in Python

 Abstraction is used to hide the internal functionality of the function


from the users.
 The users only interact with the basic implementation of the function,
but inner working is hidden.
 User is familiar with that "what function does" but they don't
know "how it does."
 In simple words, we all use the smartphone and very much familiar
with its functions such as camera, voice-recorder, call-dialing, etc., but
we don't know how these operations are happening in the background.
 Let's take another example - When we use the TV remote to increase
the volume. We don't know how pressing a key increases the volume
of the TV.
 We only know to press the "+" button to increase the volume.

That is exactly the abstraction that works in the object-oriented concept.

Why Abstraction is Important?


 In Python, an abstraction is used to hide the irrelevant data/class in
order to reduce the complexity.
 It also enhances the application efficiency. Next, we will learn how we
can achieve abstraction using the Python program.

Abstraction classes in Python


 In Python, abstraction can be achieved by using abstract classes and
interfaces.
 A class that consists of one or more abstract method is called the
abstract class.
 Abstract methods do not contain their implementation.
 Abstract class can be inherited by the subclass and abstract method
gets its definition in the subclass.
 Abstraction classes are meant to be the blueprint of the other class.
 An abstract class can be useful when we are designing large functions.
 An abstract class is also helpful to provide the standard interface for
different implementations of components.
 Python provides the abc module to use the abstraction in the Python
program. Let's see the following syntax.
Syntax

1. from abc import ABC


2. class ClassName(ABC):

We import the ABC class from the abc module.

Abstract Base Classes


 An abstract base class is the common application program of the
interface for a set of subclasses.
 It can be used by the third-party, which will provide the
implementations such as with plugins.
 It is also beneficial when we work with the large code-base hard to
remember all the classes.

Working of the Abstract Classes


 Unlike the other high-level language, Python doesn't provide the
abstract class itself.
 We need to import the abc module, which provides the base for
defining Abstract Base classes (ABC).
 The ABC works by decorating methods of the base class as abstract.
 It registers concrete classes as the implementation of the abstract
base.
 We use the @abstractmethod decorator to define an abstract
method or if we don't provide the definition to the method, it
automatically becomes the abstract method.
 Let's understand the following example.

Example -

1. # Python program demonstrate


2. # abstract base class work
3. from abc import ABC, abstractmethod
4. class Car(ABC):
5. def mileage(self):
6. pass
7.
8. class Tesla(Car):
9. def mileage(self):
10. print("The mileage is 30kmph")
11. class Suzuki(Car):
12. def mileage(self):
13. print("The mileage is 25kmph ")
14. class Duster(Car):
15. def mileage(self):
16. print("The mileage is 24kmph ")
17.
18. class Renault(Car):
19. def mileage(self):
20. print("The mileage is 27kmph ")
21.
22. # Driver code
23. t= Tesla ()
24. t.mileage()
25.
26. r = Renault()
27. r.mileage()
28.
29. s = Suzuki()
30. s.mileage()
31. d = Duster()
32. d.mileage()

Output:

The mileage is 30kmph


The mileage is 27kmph
The mileage is 25kmph
The mileage is 24kmph

Explanation -

 In the above code, we have imported the abc module to create the
abstract base class. We created the Car class that inherited the ABC
class and defined an abstract method named mileage().
 We have then inherited the base class from the three different
subclasses and implemented the abstract method differently.
 We created the objects to call the abstract method.
Let's understand another example.

Example -

1. # Python program to define


2. # abstract class
3.
4. from abc import ABC
5.
6. class Polygon(ABC):
7.
8. # abstract method
9. def sides(self):
10. pass
11.
12. class Triangle(Polygon):
13.
14.
15. def sides(self):
16. print("Triangle has 3 sides")
17.
18. class Pentagon(Polygon):
19.
20.
21. def sides(self):
22. print("Pentagon has 5 sides")
23.
24. class Hexagon(Polygon):
25.
26. def sides(self):
27. print("Hexagon has 6 sides")
28.
29. class square(Polygon):
30.
31. def sides(self):
32. print("I have 4 sides")
33.
34. # Driver code
35. t = Triangle()
36. t.sides()
37.
38. s = square()
39. s.sides()
40.
41. p = Pentagon()
42. p.sides()
43.
44. k = Hexagon()
45. K.sides()

Output:

Triangle has 3 sides


Square has 4 sides
Pentagon has 5 sides
Hexagon has 6 sides

Explanation -

 In the above code, we have defined the abstract base class named
Polygon and we also defined the abstract method.
 This base class inherited by the various subclasses.
 We implemented the abstract method in each subclass.
 We created the object of the subclasses and invoke
the sides() method.
 The hidden implementations for the sides() method inside the each
subclass comes into play. The abstract method sides() method,
defined in the abstract class, is never invoked.

Points to Remember
Below are the points which we should remember about the abstract base
class in Python.
o An Abstract class can contain the both method normal and abstract
method.
o An Abstract cannot be instantiated; we cannot create objects for the
abstract class.

Abstraction is essential to hide the core functionality from the users. We


have covered the all the basic concepts of Abstraction in Python.

You might also like