Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say I have an animal class...with fields of name, height and weight. I want to create a bird class which is an animal, but it has also, say, wing size. How can I do that?

My general idea is creating the animal class, and the bird class which inherits animal, and has additional field ...but then different kinds of birds would inherit that class.

My Questions:

  • Is my solution an appropriate OO approach to this type of problem?
  • Is there a known design pattern for handling data modeling problems of this type?
share|improve this question
Focus on using OO to solve implementation problems as they arise -- usually this means either eliminating code duplication or eliminating unwanted dependencies. With experience, you will be able to foresee implementation problems and arrive at a good solution more quickly. – kevin cline Mar 18 '13 at 21:00
whatever wing is, it sure has to somehow "override"/"inherit" particular fin. "A wing is a type of fin..." (Wikipedia) – gnat Mar 19 '13 at 16:57

You would only add members to your Bird class that are common to all birds. If you want to add a member called Fly(), you must either create a FlyingBird class that inherits from Bird, or inherit from an IFlying interface and implement the Fly() method on it.

public class Bird : Animal
    public float WingSize;

public Interface IFlyable
    void Fly()
    void Land();

public class FlyingBird : Animal, IFlyable
    public void Fly()
         // Flap wings
share|improve this answer
I do not need the fly member. maybe a getter setter for the wingSize. Oh and thank you! – user1050389 Mar 18 '13 at 18:57
Don't forget, some birds that cannot fly still have wings. :) – Robert Harvey Mar 18 '13 at 19:04

Yes, you add such a field to a derived class, just like you described. All birds have wings.

It is useful to contemplate how you will use this property. People new to OO, and especially oldsters who programmed way before the early OO days, may want to write code like (not in any particular language):

for all Animals a in someListOfAnimals:
   print, " has weight of ", a.weight
   if a is a Bird,
       print " and a wingspan of ", a.wingsize

but that is seriously wrong in OO design. You must always try to think at the right level of abstraction. The right way will be something more like

for all Animals a in someListOfAnimals:
   print a.descripion()

and define a virtual "description()" method for Animal and for Bird. The Bird.description() method will, of course, print out the wingsize along with the usual stuff. Animal.description() doesn't mention wingsize.

In case someday you run across some odd species of bird without wings, no need to make a tangled hierarchy of classes. The real world is full of exceptions. Just set wing size to zero, or some bogus value, or add a boolean haswings member.

share|improve this answer
Thanks for the advice! – user1050389 Mar 18 '13 at 20:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.