I've been instructed to create two methods that overload eachother. One should store an object in one array, and the other should store an object in the other array. The assignment reads:
Redo your Zoo so that it has the ability to storing wild animals and domestic animals separately. Your Zoo class will therefore probably/presumably have two different add() methods. One that receives an Animal-object that implements the interface for wildlife and an add() method that receives Animal objects that implement the interface for domestic animals.
How do I do this?
Class Zoo {
...
void add(Animal foobar instanceof Wild){
[...]
}
[...]
will not compile. Nor does
Class Zoo {
...
void add(Animal foobar implements Wild){
[...]
}
[...]
One could, in reality, use instanceof to do an if statement in add(), but he is specifically asking for two add() methods that overload eachother. How do I do this? Or, is this impossible, and my teach is trying to mess with me by saying probably/presumably?
The key is to provide two add()
methods, one that takes one type of parameter (a wildlife animal type) and another one that takes the other type of parameter (a domestic animal type).
You should have enough information to code this now.
Addendum (March 2016)
For example, this is the kind of code you could use:
class Animal { ... }
interface Wildlife { ... }
interface Domestic { ... }
class WildAnimal
extends Animal
implements Wildlife
{ ... }
class DomesticAnimal
extends Animal
implements Domestic
{ ... }
class Zoo
{
void add(WildAnimal beast) { ... }
void add(DomesticAnimal beast) { ... }
}
Both Zoo.add()
methods accept an argument of type Animal
. But one accepts an Animal
object that implements the Wildlife
interface, and the other accepts
an Animal
object that implements the Domestic
interface.