java - OOP Inheritance question -



java - OOP Inheritance question -

is next scenario possible:

create object pool of animal, called animalpool allocate animal object (called myanimal) animalpool cast myanimal cat set cat-specific field store myanimal array of animal (say @ index 0) later access array, , cast index 0 cat , check field set in 4.

i think can you're trying do, not way you're thinking of doing it. animalpool going animal "factory" (look mill pattern, may help, it's not of import part here.), , double collection of "animals". create "animal" object, has methods , properties mutual across animals. create animals need such "cat" , "dog" , derive them base of operations class of "animal". in "animalpool", add together functions create , homecoming specific types of animal, such getdog() , getcat(), or create 1 function parameter. create specific type animalpool factory, , because derives "animal", can add together list of type "animal". when retrieve animals animalpool list, able cast them appropriate types. depending on reflection capabilities of language, may able object tell type is.

this simple case of using inheritance , mill pattern. 2 things up, , think you'll on easy street you're trying accomplish. luck, , hope helps. can give code sample if need it. :-)

java oop inheritance

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -