Challenge 2: Implement an Animal Class
Implement an animal class and its subclasses from scratch.
We'll cover the following
Problem statement
The code below has:
-
A parent class named
Animal
.- Inside it, define:
name
sound
__init__()
Animal_details()
function- It prints the
name
andsound
of theAnimal
.
- It prints the
- Inside it, define:
-
Then there are two derived classes*
Dog
class- Has a property
family
- Has an initializer that calls the parent class initializer in it through
super()
- Has an overridden method named
Animal_details()
which prints detail of the dog.
- Has a property
Sheep
class- Has a property
color
- Has an initializer that calls the parent class initializer in it through
super()
- Has an overridden method named
Animal_details()
, which prints detail of the sheep
- Has a property
-
The derived classes should override the
Animal_details()
method defined in theAnimal
class.- The overridden method in
Dog
class should print the value offamily
as well as thename
andsound
. - The overridden method in
Sheep
class should print the value ofcolor
as well as thename
andsound
- The overridden method in
Input
-
name
ofDog
is set to Pongo,sound
is set to Woof Woof, andfamily
is set to Carnivore in the initializer ofDog
object. -
name
ofSheep
is set to Billy,sound
is set to Baaa Baaa, andcolor
is set to White in the initializer ofSheep
object. -
Now, call
Animal_details()
from their respective objects.
Sample input
d = Dog("Pongo", "Woof Woof", "Husky")
d.Animal_details()
print(" ")
s = Sheep("Billy", "Baaa Baaa", "White")
s.Animal_details()
Sample output
Name: Pongo
Sound: Woof Woof
Family: Husky
Name: Billy
Sound: Baa Baa
Color: White
Get hands-on with 1400+ tech skills courses.