Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overriding with difference access specification c++

Tags:

c++

I came across a question while taking iKM test. There was a base class with two abstract methods with private access specifier. There was a derived class which was overriding these abstract methods but with protected/public access specifier.

I never came across such thing where overridden methods in derived class had different access specification. Is this allowed ? If yes, does it comply to "IS A" relation between base and derived (i.e. safely substitutable).

Could you point me to some references which can provide more details on such usages of classes ?

Thank you.

like image 985
irappa Avatar asked May 21 '13 11:05

irappa


1 Answers

It is allowed, in both directions (ie, from private to public AND from public to private).

On the other hand, I would argue it does not break the IS-A relationship. I base my argument on 2 facts:

  • using a Base& (or Base*) handle, you have exactly the same interface as before
  • you could perfectly (if you wish) introduce a forward method that is public and calling the private method directly anyway: same effect with more typing
like image 189
Matthieu M. Avatar answered Oct 06 '22 00:10

Matthieu M.