abstract base class and abstract method in python code example
Example 1: python abstract class
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")
R = Human()
R.move()
K = Snake()
K.move()
R = Dog()
R.move()
K = Lion()
K.move()
Output:
I can walk and run
I can crawl
I can bark
I can roar
Example 2: Abstraction example python
from abc import ABC,abstractclassmethod
class Parent(ABC):
@abstractclassmethod
def pqr(self):
pass
class Child(Parent):
def pqr(self):
print("PQR")
obj = Child()
obj.pqr()