I am trying to model some system in Java (OOP), but I'll use a simpler example to explain the problem I'm running into (which I believe is to do with Polymorphism and/or Inheritence). Though I have a feeling that the solution may be to do with Composition.
Say simply we have Animal , Species , Bird and Pigeon . The current relationship I have is:
Though keep in mind that this design should be extendable to include more Animals and Species .
I want to do the following:
List<Animal>
, I want to be able to add Pigeon
(and other animals) only but not Species
or Bird
. This is satisfied.List<Bird>
, I want to be able to add Pigeon
(and other birds) only. This is satisfied.List<Species>
, I want to be able to add Bird
(and other species) only; however, by polymorphism of this design, I'm able to add Pigeon
too which is undesirable (I want it such that Pigeon is not a Species in this example).I never understood this entirely, but I believe this runs into the issues with inheritence that I've heard so much about. I hope I've explained the example in sufficient detail, but if anyone has any suggestions as to how to model this at all (with abstract classes, interfaces, composition, design patterns, anything), that would be very much appreciated!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.