Abstraction in Python Example-DecodingDevOps

Abstraction in Python Example-DecodingDevOps

Abstraction is an important aspect of object-oriented programming. In this process of abstraction, internal details are hidden and functionality is checked in order to reduce the complexity and increase efficiency. we can also perform data hiding by adding the double underscore (___) as a prefix to the attribute which is to be hidden.  Abstraction is accomplished using abstract classes and interfaces. Abstract methods are the methods that generally don't have any implementation, it is left to the subclasses to provide an implementation for the abstract methods. it is simply a work of art where the subject or the theme is implied. A class that contains one or more abstract methods is called the abstract class.  Subclasses should provide an 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 an abstract class.

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

Leave a Reply

Your email address will not be published. Required fields are marked *