Skip to content

Commit ecee342

Browse files
Create Abstract classes.py
1 parent fc27f5d commit ecee342

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

23 OOPs 2/Abstract classes.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'''
2+
Problem statement
3+
Python program that demonstrates the use of abstract base classes and inheritance. The program defines an abstract base class Animal with an abstract method move, and several concrete classes that inherit from Animal and implement the move method with different behaviors.
4+
5+
Note:
6+
1. An abstract base class Animal with an abstract method move.
7+
2. Five concrete classes that inherit from the Animal class, each representing a different type of animal:
8+
Human : I can walk and run
9+
Snake : I can crawl
10+
Dog : I can bark
11+
Lion : I can roar
12+
3. Each concrete class should provide an implementation for the move method, displaying a message specific to that type of animal's movement.
13+
Output :
14+
I can walk and run
15+
I can crawl
16+
I can bark
17+
I can roar
18+
'''
19+
20+
from abc import ABC, abstractmethod
21+
class Animal(ABC):
22+
@abstractmethod
23+
def move(self):
24+
pass
25+
26+
# write your code logic
27+
28+
class Human(Animal):
29+
def move(self):
30+
print("I can walk and run")
31+
32+
class Snake(Animal):
33+
def move(self):
34+
print("I can crawl")
35+
36+
37+
38+
class Dog(Animal):
39+
def move(self):
40+
print("I can bark")
41+
42+
43+
44+
class Lion(Animal):
45+
def move(self):
46+
print("I can roar")
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
R = Human()
61+
R.move()
62+
63+
K = Snake()
64+
K.move()
65+
66+
R = Dog()
67+
R.move()
68+
69+
K = Lion()
70+
K.move()

0 commit comments

Comments
 (0)