xxxxxxxxxx
from abc import ABC, abstractmethod
class MyBaseClass(ABC):
@abstractmethod
def my_abstract_method(self):
pass
class MySubClass(MyBaseClass):
def my_abstract_method(self):
print("Implementation of the abstract method")
# Attempting to create an instance of the abstract class will raise an error
# my_instance = MyBaseClass() # This line will raise a TypeError
# Creating an instance of the subclass and calling the abstract method
my_instance = MySubClass()
my_instance.my_abstract_method()
xxxxxxxxxx
# Python program showing
# abstract base class work
from abc import ABC, abstractmethod
class Polygon(ABC):
@abstractmethod
def noofsides(self):
pass
class Triangle(Polygon):
# overriding abstract method
def noofsides(self):
print("I have 3 sides")
class Pentagon(Polygon):
# overriding abstract method
def noofsides(self):
print("I have 5 sides")
class Hexagon(Polygon):
# overriding abstract method
def noofsides(self):
print("I have 6 sides")
class Quadrilateral(Polygon):
# overriding abstract method
def noofsides(self):
print("I have 4 sides")
# Driver code
R = Triangle()
R.noofsides()
K = Quadrilateral()
K.noofsides()
R = Pentagon()
R.noofsides()
K = Hexagon()
K.noofsides()
xxxxxxxxxx
# 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]
xxxxxxxxxx
An abstract class exists only so that other "concrete" classes can inherit from the abstract class.
xxxxxxxxxx
from abc import ABC, abstractmethod
class AbstractClassExample(ABC):
@abstractmethod
def method1(self):
pass
@abstractmethod
def method2(self):
pass
class ConcreteClassExample(AbstractClassExample):
def method1(self):
print("Implementation of method1")
def method2(self):
print("Implementation of method2")
# Trying to instantiate an abstract class will result in a TypeError
# abstract_class = AbstractClassExample()
# We can instantiate and use a concrete class derived from the abstract class
concrete_class = ConcreteClassExample()
concrete_class.method1() # Output: Implementation of method1
concrete_class.method2() # Output: Implementation of method2
xxxxxxxxxx
# Python program showing
# abstract base class work
from abc import ABC, abstractmethod
class Animal(ABC):
def move(self):
pass
class Human(Animal):
def move(self):
print("I can walk and run")
class Snake(Animal):
def move(self):
print("I can crawl")
class Dog(Animal):
def move(self):
print("I can bark")
class Lion(Animal):
def move(self):
print("I can roar")
# Driver code
R = Human()
R.move()
K = Snake()
K.move()
R = Dog()
R.move()
K = Lion()
K.move()