Thursday, June 30, 2022

Abstraction in Python

Abstraction is one of the four fundamental OOPS concepts. The other three being-

What is Abstraction

Abstraction means hiding the complexity and only showing the essential features of the object. So in a way, Abstraction means hiding the real implementation and we, as a user, knowing only how to use it.

Real world example would be a vehicle which we drive with out caring or knowing what all is going underneath.

A TV set where we enjoy programs with out knowing the inner details of how TV works.

Abstraction in Python

Abstraction in Python is achieved by using abstract classes and interfaces.

An abstract class is a class that generally provides incomplete functionality and contains one or more abstract methods. Abstract methods are the methods that generally don’t have any implementation, it is left to the sub classes to provide implementation for the abstract methods.

Refer this post Abstract Class in Python to know more about abstract class in Python.

An interface should just provide the method names without method bodies. Subclasses should provide implementation for all the methods defined in an interface. Note that in Python there is no support for creating interfaces explicitly, you will have to use abstract class. In Python you can create an interface using abstract class. If you create an abstract class which contains only abstract methods that acts as an interface in Python.

Python abstraction example using abstract class

In the example there is an abstract class Payment that has an abstract method payment(). There are two child classes CreditCardPayment and MobileWalletPayment derived from Payment that implement the abstract method payment() as per their functionality.

As a user we are abstracted from that implementation when an object of CreditCardPayment is created and payment() method is invoked using that object, payment method of CreditCardPayment class is invoked. When an object of MobileWalletPayment is created and payment() method is invoked using that object, payment method of MobileWalletPayment class is invoked.

from abc import ABC, abstractmethod
class Payment(ABC):
  def print_slip(self, amount):
    print('Purchase of amount- ', amount)
  @abstractmethod
  def payment(self, amount):
    pass

class CreditCardPayment(Payment):
  def payment(self, amount):
    print('Credit card payment of- ', amount)

class MobileWalletPayment(Payment):
  def payment(self, amount):
    print('Mobile wallet payment of- ', amount)

obj = CreditCardPayment()
obj.payment(100)
obj.print_slip(100)
print(isinstance(obj, Payment))
obj = MobileWalletPayment()
obj.payment(200)
obj.print_slip(200)
print(isinstance(obj, Payment))

Output

Credit card payment of-  100
Purchase of amount-  100
True
Mobile wallet payment of-  200
Purchase of amount-  200
True

That's all for this topic Abstraction in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Method Overriding in Python
  2. Multiple Inheritance in Python
  3. Local, Nonlocal And Global Variables in Python
  4. pass Statement in Python
  5. Check if String Present in Another String in Python

You may also like-

  1. Name Mangling in Python
  2. Magic Methods in Python With Examples
  3. User-defined Exceptions in Python
  4. Convert String to int in Python
  5. Difference Between Encapsulation And Abstraction in Java
  6. Object Creation Using new Operator in Java
  7. Spring MVC Form Example With Bean Validation
  8. Uber Mode in Hadoop