By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

OOP design question

P: n/a
The issue is having a homogenous collection of base objects, but
needing to perform class-specific functions only on those of a
specific type inside it.

Cat, Dog, and Bird all derive from the Animal class
There is an array, Animal[]
Only Dog can Fetch().
I need to cause all dogs in Animal to Fetch()

It's been said that using instanceof is the sign of a bad design.
However, my solution was basically:

1) Go through Animal[] and create a list of instanceof Dog
2) Go through the Dog list and call Fetch()

Is there a better way? It doesn't really make sense to make
Animal.Fetch() and then only Dog override it.
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Jeramie" <Je**********@hotmail.com> wrote in message
news:58**************************@posting.google.c om...
The issue is having a homogenous collection of base objects, but
needing to perform class-specific functions only on those of a
specific type inside it.

Cat, Dog, and Bird all derive from the Animal class
There is an array, Animal[]
Only Dog can Fetch().
I need to cause all dogs in Animal to Fetch()

It's been said that using instanceof is the sign of a bad design.
However, my solution was basically:

1) Go through Animal[] and create a list of instanceof Dog
2) Go through the Dog list and call Fetch()

Is there a better way? It doesn't really make sense to make
Animal.Fetch() and then only Dog override it.


If you have this problem, you might need to rethink your design. What are
the other animals doing while your dogs are fetching? Should you maybe
define a play method in Animal? In the future, post to comp.lang.java.help
for beginner type questions or comp.lang.java.programmer for more advanced
questions. This group isn't supposed to exist and isn't carried by all
servers.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.