Class inheritance VS. class collections


im having a bit of a problem trying to plan for my solution. its a very basic drawing program with shapes.

im not exactly sure which way to hit this, these are the elements i should have in there:
a shape class - base class for any inheritence
a point class - is a single point object, like a coordinate
a line class - is a line between two 'points'
a rectangle class - is a square (consists of four lines)
a polygon class - is a 'n' sided shape (consists of 'n' lines)

the model i have so far is something like this:

Shape Class                // Base class
 Point Class                 // Inherits from Shape
 Line Class                 // Inherits from Point
     |       |______________________
     |                                                   |
Rectangle Class                  Polygon Class                 // These classes both inherit from Line class

This is something along the lines of what i was thinking, but im also thinking it wont work very well because for example the 'Rectangles' class inherits from 'Line', however in reality the 'Rec' class should have a collection of 'Line' (4 lines in a square) right?

so in this case would everything just equally inherit from 'Shape' ? or will i be able to have collections of base classes in my sub (inherited) classes?

Who is Participating?
peprConnect With a Mentor Commented:
It depends on how you want to use the objects. If you want to use polymorphism--i.e. Rectangle "is a" Shape, Polygon is a shape, Line is a Shape--and you have, for example, a collection of shapes and want to say "Draw all shapes from the collection, then each of your classes have to inherit from Shape.

But it probably does not make sense to think in terms "line is a point". Rather, "line is a shape that CONSISTS of two points". This way Line should derive from Shape but it only USES the two intenal members of the Point class (if ever). But using the two Points internally is an implementation detail. It should not be visible on the Line object from the outside.

The same way, Rectangle IS A Shape. But it is questionable whether using 4 points instead of 4 lines, or some other representation is not better or will not be better for future implementation. This way, Rectangle should derive from Shape but should ony USE the other classes (if any).

In other words, when deciding whether to use inheritance or composition, think about the derived class that "it behaves almost the same except in some special cases, special sense".

When in doubt (both solutions seems equal from the first point of view), it is recommended to use a composition instead of inheritance. It leads to simpler interfaces and easier modifications.

gem56Author Commented:
Hi pepr,

yeah that seems more logical, looks like thats the way ill fly.

Thanks for your help :)
You are welcome ;)    And thanks for <strike>all the fish</strike> the points...
>> because for example the 'Rectangles' class inherits from 'Line', however in reality the 'Rec' class should have a collection of 'Line' (4 lines in a square) right?

As pepr already said : that's exactly why you shouldn't use inheritance between a rectangle and a line (for example). You can't say "a rectangle is a line".

Anyway, an example of where you could use inheritance (I didn't say you have to, because that depends on what you want to do with the application) is :

        a square is a kind of rectangle
        a rectangle is a polygon

As I said, that doesn't necessarily warrant inheritance, because the internal representation of a square can be very different from a rectangle (the size of a square can be represented with one value width, while a rectangle needs width and height for example).
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.