Like you said previously, a Zoo has Animals, an Animal is not a Zoo. Therefore, Animals shouldn't really be a subclass of Zoo.
Your implementation still hard codes the animal types into the Zoo class. If you want to add a new type of animal, there are a lot of changes to make.
Instead, the count methods should be members of the Animal subclasses. Zoo should have an array of objects of type Animal, and be able to get the count for each one.
Zoo.pm - A zoo. This has an array that holds many Animal objects. It knows nothing about specific Animal subclasses. It can also have a factory method that creates a new animal and adds it to the zoo collection.
Animal.pm - These are Animal objects. This is a interface type class that merely defines specific methods that Zoo will use. The factory could also logically be here
Animal::Llama - Subclass of Animal that holds Llamas.
Animal::Camel - Subclass of Animal that holds Camels.
This way, a Zoo has a collection (array) of Animals. To get the counts of each animal type, try this:
my %count;
foreach my $animal in (@zooanimals) {
# $animal is a Animal object.
my $animal_name = $animal->get_name();
$count{$animal_name}++;
}
# another loop here to print %count.
Get it? The Zoo should hold Animals, and know nothing about subtypes of animals.
Exception: - The factory method can know about animal types. That way it can create the appropriate subclass.
sub add_animal() {
my $type = shift;
if($type == "Llama") {
my $newanimal = new Animal::Llama;
} elsif {$type == "Camel") {
my $newanimal = new Animal::Camel;
}
push (@allanimals, $newanimal);
}
my $zoo = new Zoo;
$zoo->add_animal("Llama");
$zoo->add_animal("Llama");
$zoo->add_animal("Camel");
$zoo->count_animals();
__OUTPUT__
There are 2 Llamas in the zoo.
There are 1 Camels in the zoo.
This is rough and incomplete. (I would add a Animal::Collection object - a zoo has many collections of animals. A collection is a number of individual animals.) But, this is a better design.
To add a new animal type, you just make two changes - the new class, and add it to the factory method.
Is this clear? I'm happy to clarify if you still need help.