Valeri Grishin
Valeri Grishin

Reputation: 102

inheritance of Has-a relation

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();

};

Upvotes: 1

Views: 91

Answers (1)

LogicStuff
LogicStuff

Reputation: 19607

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.

Upvotes: 2

Related Questions