Why C++ allows to give more restrictive access to a derived class method?

174 Views Asked by At

This link talks about allowing more restrictive access to a derived class method.

Q. What is the reason for allowing this in C++?

Languages such as Java & C# don't allow it. Is it useful in some cases? If so please help me understand.

1

There are 1 best solutions below

0
On BEST ANSWER

It has more to do with it never being disallowed. And now it's too late: too much code would break. Do remember that C++ is a considerably older language than either Java or C#.

But the C++ philosophy inspires you to ask "why disallow it?". It could even be useful: some folk exploit it and make overridden methods private. The amount of documentation you should attach to a private method can be significantly less than a public method. This means you don't repeat yourself and are compelled to rely on the public / protected method in a base class for comments.