abstract class pythn code example
Example 1: abstract method python
from abc import ABC, abstractmethod
class AbstractClassExample(ABC):
def __init__(self, value):
self.value = value
super().__init__()
@abstractmethod
def do_something(self):
pass
Example 2: 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 3: abstarct class python
import abc
class Shape(metaclass=abc.ABCMeta):
@abc.abstractmethod
def area(self):
pass
class Rectangle(Shape):
def __init__(self, x,y):
self.l = x
self.b=y
def area(self):
return self.l*self.b
r = Rectangle(10,20)
print ('area: ',r.area())