# Abstract class :
# A class with no constructor since it cannot be instantiated, extends ABC and has both abstract and concrete methods
# Any class that extends this abstract class must override the abstract methods
# The concrete methods are inherited by the child class
# Interface: This is nothing but a variation of abstract class where there is no concrete methods, all methods are abstract methods
###### Example of abstract class
from abc import ABC, abstractmethod
class Company(ABC):
@abstractmethod
def pay_taxes(self):
pass
def report_revenue(self):
print(f"{self.name} is reporting ${self.revenue} of revenue")
class Manufacturing(Company):
def __init__(self, name, revenue):
self.name = name
self.revenue = revenue
def pay_taxes(self, tax_rate): # Implement the pay_taxes() method
tax_amount = self.revenue * tax_rate
print(f"{self.name} is paying ${tax_amount} of taxes")
# Create an instance of the Manufacturing class and Make call to the pay_taxes() method, observe report_revenue()
m = Manufacturing("Morgan's Manufacturing", 5000)
m.pay_taxes(0.1)
m.report_revenue()
###### Example of interface (All methods in the abstract class must be abstract methods)
class Business(ABC):
@abstractmethod
def sell_product(self, product_name, price, quantity):
pass
# Create a class that inherits the Business interface
class Bakery(Business):
def __init__(self, business_name):
self.business_name = business_name
# Provide a definition of the sell_product() method
def sell_product(self, price, quantity, product_name):
total_revenue = price * quantity
print(f"""{self.business_name} sold {quantity} {product_name} for a total of ${total_revenue}""")
blue_eyed_baker = Bakery("Blue Eyed Baker") # Attempt to create a Bakery object
##### Example of Interface without explicitly using the "@abstractmethod" decorator : Duck typing approach
class Supplier:
def take_order(self, product_name, quantity):
pass
def make_delivery(self, order_id, location):
pass
class YogurtSupplier:
def __init__(self):
self.orders = {}
# Finish defining the take_order() method
def take_order(self, product_name, quantity):
self.orders[f"{product_name}_{quantity}"] = {
"product_name": product_name, "quantity": quantity
}
# Create a make_delivery() abstract method
def make_delivery(self, order_id, location):
print(f"Delivering order: {order_id} to {location}")
del self.orders[order_id]