1# Python program showing
2# abstract base class work
3
4from abc import ABC, abstractmethod
5class Animal(ABC):
6
7 def move(self):
8 pass
9
10class Human(Animal):
11
12 def move(self):
13 print("I can walk and run")
14
15class Snake(Animal):
16
17 def move(self):
18 print("I can crawl")
19
20class Dog(Animal):
21
22 def move(self):
23 print("I can bark")
24
25class Lion(Animal):
26
27 def move(self):
28 print("I can roar")
29
30# Driver code
31R = Human()
32R.move()
33
34K = Snake()
35K.move()
36
37R = Dog()
38R.move()
39
40K = Lion()
41K.move()
42
43Output:
44
45I can walk and run
46I can crawl
47I can bark
48I can roar
1import abc
2class Shape(metaclass=abc.ABCMeta):
3 @abc.abstractmethod
4 def area(self):
5 pass
6class Rectangle(Shape):
7 def __init__(self, x,y):
8 self.l = x
9 self.b=y
10 def area(self):
11 return self.l*self.b
12r = Rectangle(10,20)
13print ('area: ',r.area())
1from abc import ABC,abstractclassmethod
2
3class Parent(ABC):
4
5 @abstractclassmethod
6 def pqr(self):
7 pass
8
9class Child(Parent):
10
11 def pqr(self):
12 print("PQR")
13
14obj = Child()
15obj.pqr()
1An abstract class exists only so that other "concrete" classes can inherit from the abstract class.