Access directionality: Because a derived class can access public members of its base class, does it follow that base-class member functions can access public members of the derived class? Provide the correct judgment.

Difficulty: Easy

Correct Answer: Incorrect — access does not work upward; base members do not gain access to derived members.

Explanation:


Introduction / Context:
The statement probes a common misunderstanding: that accessibility is symmetric across inheritance. In reality, inheritance grants the derived class visibility into the base’s accessible members, not vice versa. Base classes do not magically see derived members simply because a derived type exists.


Given Data / Assumptions:

  • Base has public/protected members; derived has its own members.
  • We consider member function bodies defined inside the base type itself.
  • No friend declarations or CRTP tricks are assumed.


Concept / Approach:
Access control is independent of object identity. A base-class member function is compiled in the context of Base, so it can access members declared in Base (per access specifiers) but not those declared later in Derived. While you can pass a Derived instance to a base function, that does not change what members the code is permitted to access. Virtual dispatch lets a derived override change behavior when called via a base interface, but it does not alter access rights of base code to derived data.


Step-by-Step Solution:

Define Base::f() — it can use Base’s public/protected/private (subject to rules) but not Derived’s members. Call f() via a Derived object — Base::f() still cannot name Derived’s members. Only friend relationships or downcasts with explicit knowledge of Derived (and accessibility) change what can be accessed, and even then, access rules still apply.


Verification / Alternative check:
Try to compile Base::f() referencing a Derived-only field; the compiler errors because the name is unknown in Base’s scope. Overriding a virtual in Derived does not grant Base visibility into Derived members.


Why Other Options Are Wrong:

  • “Public implies universal access up and down”: false; access is lexical and based on declaring scope.
  • “Only when virtual” or “only with references”: dynamic dispatch affects behavior resolution, not access control.


Common Pitfalls:
Equating polymorphism (which member executes) with access rights (what names are visible within code).


Final Answer:
Incorrect — access does not work upward; base members do not gain access to derived members.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion