java - How do you overload dependent on what an object is an instance of? -
i've been instructed create 2 methods overload eachother. 1 should store object in 1 array, , other should store object in other array. assignment reads:
redo zoo has ability storing wild animals , domestic animals separately. zoo class therefore probably/presumably have 2 different add() methods. 1 receives animal-object implements interface wildlife , add() method receives animal objects implement interface domestic animals.
how 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 if statement in add(), asking 2 add() methods overload eachother. how do this? or, impossible, , teach trying mess me saying probably/presumably?
the key provide 2 add() methods, 1 takes 1 type of parameter (a wildlife animal type) , 1 takes other type of parameter (a domestic animal type).
you should have enough information code now.
addendum (march 2016)
for example, kind of code 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 argument of type animal. 1 accepts animal object implements wildlife interface, , other accepts animal object implements domestic interface.
Comments
Post a Comment