Python Abstraction
Table Of Contents:
- What Is Abstraction?
- Need Of Abstraction?
- Implementing Abstraction In Python.
- Abstract Base Class.
- Abstract Class.
- Abstract Method.
- Concrete Methods In Abstract Base Class.
- Abstract Class Instantiation.
(1) What Is Abstraction?
- Abstraction helps us to hide the internal implementation of the function from the users.
- Hence the user can use that function without knowing the internal implementation.
- User is familiar with that “what function does” but they don’t know “how it does.”
(2) Need Of Abstraction?
- Abstraction provides a programmer to hide all the irrelevant data/processes of an application in order to reduce complexity and increase the efficiency of the program.
- Abstraction helps us to provide security, by hiding internal sensitive data from users.
- Abstraction enables users to use the functionality without any hassle.
(3) Implementing Abstraction In Python?
- In Python abstraction can be achieved by using abstract class and abstract method.
- Before that, we need to understand what is ‘Abstract Base Class’.?
Abstract Base Class:
- In Python, we don’t have any keyword to indicate an abstract class.
- Python comes with a module that provides the base for defining Abstract Base classes(ABC).
- The module name is ‘ABC’.
- The ‘ABC’ module implements abstraction by getting inherited in the base class and by decorating methods of the base class as abstract.
Syntax:
from abc import ABC
class MyClass(ABC):
Abstract Class:
- A class that consists of one or more abstract methods is called the abstract class.
- Abstract methods do not contain their implementation. They are just empty functions.
- The subclasses can inherit an abstract class and can define empty abstract methods.
- We have to inherit the ‘abc’ module from Python to implement Abstraction.
Syntax:
from abc import ABC
class MyClass(ABC):
Note:
- Here ‘MyClass’ is the abstract class because it has inherited Python’s ‘ABC’ class.
- It also has an abstract method ‘mymethod‘ , which doesn’t have any implementation.
Abstract Method:
- A method becomes abstract when decorated with the keyword @abstractmethod.
Syntax:
from abc import ABC,abstractmethod
class MyClass(ABC):
@abstractmethod
def mymethod(self):
pass
Example:
from abc import ABC,abstractmethod
class Bikes(ABC):
@abstractmethod
def milage(self):
pass
class Honda(Bikes):
def milage(self):
print('Honda Milage = 50')
class Hero(Bikes):
def milage(self):
print('Hero Milage = 40')
class Suzuki(Bikes):
def milage(self):
print('Suzuki Milage = 30')
class SuperBike(Bikes):
def milage(self):
print('SuperBike Milage = 20')
Creating Objects Of The Class.
bike1 = Honda()
bike2 = Hero()
bike3 = Suzuki()
bike4 = SuperBike()
Calling The Abstract Method:
bike1.milage()
bike2.milage()
bike3.milage()
bike4.milage()
Output:
Honda Milage = 50
Hero Milage = 40
Suzuki Milage = 30
SuperBike Milage = 20
(4) Concrete Method In Abstract Base Class.
- A Concrete Method is a method having a method body. That means it has the implementation part inside it.
- Where as an Abstract Method doesn’t have any method body.
- Concrete classes contain only concrete methods whereas abstract classes may contain both concrete methods and abstract methods.
Example:
from abc import ABC,abstractmethod
class Mobile(ABC):
def OS(self):
print('Android OS')
class Samsung(Mobile):
def OS(self):
super().OS()
def model(self):
print('Samsung Mobile')
class OnePlus(Mobile):
def OS(self):
super().OS()
def model(self):
print('OnePlus Mobile')
class HTC(Mobile):
def OS(self):
super().OS()
def model(self):
print('HTC Mobile')
Creating Objects Of The Class.
mobile1 = Samsung()
mobile2 = OnePlus()
mobile3 = HTC()
Calling The Abstract Method:
mobile1.OS()
mobile1.model()
mobile2.OS()
mobile2.model()
mobile3.OS()
mobile3.model()
Output:
Android OS
Samsung Mobile
Android OS
OnePlus Mobile
Android OS
HTC Mobile
(5) Abstract Class Instantiation.
- Abstract classes are the template for the other classes to use, it does not have any implementation on them.
- Hence if you create an object of the abstract class, it will be of no use.
- If you create an object of the Abstract Class it will through you an error, TypeError: Can’t instantiate abstract class
Example:
from abc import ABC,abstractmethod
class Student(ABC):
@abstractmethod
def name(self):
pass
Creating Objects Of The Class.
obj = Student()
Output:
TypeError Traceback (most recent call last)
Input In [97], in <cell line: 1>()
----> 1 obj = Student()
TypeError: Can't instantiate abstract class Student with abstract method name