1

I was reading about Has-a and Is-a relation in here: What do “has-a” and “is-a” mean? [duplicate] . What I understand is that my data members of the class follow the Has-a relation. Let's say that I inherit data members from the Base class. Are they still going to follow Has-a relation? in this example: Car is-a vehicle. Is it still going to have a steering wheel?

class SteeringWheel
{};

class Vehicle
{

public:
SteeringWheel  sWheel;
virtual void doStuff() = 0;

};

class Car: public Vehicle
{

virtual void doStuff();

};

1 Answers1

2

Yes, both is-a and has-a relations are transitional. That means, you cannot say, for example, that

Car is-a Vehicle (which has-a SteeringWheel) except it does not have-a SteeringWheel.

or

Car is-a Vehicle (which is-a Machine) except it is-not-a Machine.

Having to implement the above examples would mean that your design is flawed. This would essentially go against the Open-closed principle in SOLID - you simply should not take away from a base class (or ignore the existence of some of its parts), only extend it by inheritance.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • Thank you for your answer. What i am trying to understand is why the example above goes against Open-closed principle. If we would like to extend the inheritance, then all the vehicles will have a steering wheel without changing the base class. what am i missing? – Valeri Grishin Mar 05 '19 at 13:08
  • 1
    @ValeriGrishin LogicStuff didn't say you are wrong (or that your example violates open-closed), they just provided some examples of where problems could occur. As long as all your vehicles are guaranteed to have steering wheels, having a base class `Vehicle` with a member `SteeringWheel` is perfectly fine. But note that `AirBalloon` should not inherit from `Vehicle` then because `AirBalloon` should not have a `SteeringWheel` member. – Max Langhof Mar 05 '19 at 13:21