Consider the following scenario:
You are tasked with designing a software system for a zoo. The zoo has various types of animals, each with different characteristics and abilities. The animals can be classified into two main categories: mammals and birds.
You need to design a class hierarchy that represents the animals in the zoo. The base class is Animal
, which has the following attributes:
name
: a string representing the name of the animal.age
: an integer representing the age of the animal.The Animal
class has the following methods:
getName()
: returns the name of the animal.getAge()
: returns the age of the animal.makeSound()
: prints a generic sound made by the animal.The mammals in the zoo have some additional characteristics. They have a furColor
attribute and a numberOfLegs
attribute. The birds have an isSwimmer
attribute and a wingSpan
attribute. The mammals and birds also have their specialized behavior, but the makeSound()
method is common for all animals.
Your task is to design the class hierarchy to represent this zoo system, with proper inheritance and encapsulation principles. Implement the necessary classes and methods to achieve this.
Write the code for the classes Animal
, Mammal
, Bird
, and Tiger
according to the given design requirements.
Animal
Classpublic class Animal {
private String name;
private int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void makeSound() {
System.out.println("[Generic sound made by the animal]");
}
}
Mammal
Classpublic class Mammal extends Animal {
private String furColor;
private int numberOfLegs;
public Mammal(String name, int age, String furColor, int numberOfLegs) {
super(name, age);
this.furColor = furColor;
this.numberOfLegs = numberOfLegs;
}
public String getFurColor() {
return furColor;
}
public int getNumberOfLegs() {
return numberOfLegs;
}
public void makeSound() {
System.out.println("[Mammal-specific sound]");
}
}
Bird
Classpublic class Bird extends Animal {
private boolean isSwimmer;
private double wingSpan;
public Bird(String name, int age, boolean isSwimmer, double wingSpan) {
super(name, age);
this.isSwimmer = isSwimmer;
this.wingSpan = wingSpan;
}
public boolean isSwimmer() {
return isSwimmer;
}
public double getWingSpan() {
return wingSpan;
}
public void makeSound() {
System.out.println("[Bird-specific sound]");
}
}
Tiger
Classpublic class Tiger extends Mammal {
public Tiger(String name, int age, String furColor, int numberOfLegs) {
super(name, age, furColor, numberOfLegs);
}
public void makeSound() {
System.out.println("[Tiger-specific sound]");
}
}
The given code implements the class hierarchy to represent the animals in the zoo. The Animal
class is the base class with common attributes and methods. The Mammal
and Bird
classes inherit from the Animal
class and add their specialized attributes and behaviors. The Tiger
class further extends the Mammal
class and overrides the makeSound()
method.
This design allows for polymorphism, where objects of different types can be treated as objects of the base class Animal
. For example, you can have an array of Animal
objects and call makeSound()
on each one, and the appropriate makeSound()
method will be called depending on the actual type of the object. This enables flexibility in working with different types of animals in the zoo system.
Note: The code provided above is in Java, but the concept of inheritance and polymorphism applies to other programming languages as well.