I read (Scott Myers) that inheritance breaks encapsulation. When data/internal methods are private (not protected), is encapsulation broken?
e.g.
class Vehicle
{
int color;
public:
void set_color();
int get_color();
}
class Car: public Vehicle
{
public:
void change_tires();
}
I can change the internals of Vehicle without breaking Car ever knowing, correct?
Phrases like “inheritance breaks encapsulation” are often taken out of context.
What the phrase refers to is the fact that the virtual interface being inherited is now not encapsulated. In effect, inheritance means that certain things that were hidden are now exposed. Things like
protectedmembers andvirtualprivatemembers are all open for play by derived classes.Non-
virtualprivatemembers are still hidden.