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 protected members and virtual private members are all open for play by derived classes.
Non-virtual private members are still hidden.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With