I have 3 classes. They have all similar integers, methods etc…
class Country{
int a,b;
public:
Country();
void doSomething();
:
:
}
class Military : public Country {
public:
Power();
void doAnother();
void doAnother2();
:
:
}
class Technology : public Military{
public:
Technology();
void doAnother3();
:
:
}
Assume that this inheritance suits my solution. But as you see, when i create Military from Country, there is not a relation between them logically. I mean, military is not a country. Also for Technology from country, the problem is same. Technology is not a Military nor Country.
Anyway, that solution is okay for me, it shortens my code, but if i do this, do i betray Object Oriented Programming Philosophy? Is it a contradicton?
Yes, it’s against OOP. You relationships are
has-a, notis-a.You should use composition instead of inheritance for this.