How do I make the method return type generic?

前端 未结 19 2429
無奈伤痛
無奈伤痛 2020-11-22 06:16

Consider this example (typical in OOP books):

I have an Animal class, where each Animal can have many friends.
And subclasses like

相关标签:
19条回答
  • 2020-11-22 06:49

    Not really, because as you say, the compiler only knows that callFriend() is returning an Animal, not a Dog or Duck.

    Can you not add an abstract makeNoise() method to Animal that would be implemented as a bark or quack by its subclasses?

    0 讨论(0)
提交回复
热议问题