Library reuse: Can we derive a class from a base class even if the base class’s source code is not available (for example, only a header, binary, or assembly/metadata is provided)? Choose the best evaluation.

Difficulty: Easy

Correct Answer: Correct — derivation needs the interface/type, not necessarily the full source.

Explanation:


Introduction / Context:
Reuse through inheritance often targets library types. In both C++ and C#, you can derive from classes defined in precompiled libraries as long as the compiler and linker (or runtime) can see the necessary interface/type information. Source code access is not required for inheritance to function.


Given Data / Assumptions:

  • C++: a header with declarations and a linked library/object providing definitions.
  • C#: an assembly (DLL) exposing public/protected members via metadata.
  • No private constructor-only bases or sealed classes preventing derivation.


Concept / Approach:
Compilers need type declarations (signatures, visibility, vtable layout in C++ per ABI) to compile derived classes. Linkers/CLR/JIT resolve definitions at build/run time. If the base class permits derivation (not final in C++ or sealed in C#), and the constructor/protected interface is accessible, you can derive without the source. This is standard practice when extending frameworks and SDKs.


Step-by-Step Solution:

Reference the base type declaration (header/assembly). Declare your derived class and override/extend as allowed. Build/link against the binary providing the base implementation.


Verification / Alternative check:
Many UI frameworks (MFC, .NET WinForms/WPF) and game engines encourage subclassing library classes without supplying their source code to developers.


Why Other Options Are Wrong:

  • Requiring source: unnecessary; declarations/metadata suffice.
  • “Only if abstract” or “only with friends”: derivation rules are not restricted to these cases.
  • Reflection is unrelated to normal inheritance.


Common Pitfalls:
Confusing the availability of symbol definitions at link/runtime with the need for source code; or forgetting sealed/final restrictions that can disallow derivation even if the type is visible.


Final Answer:
Correct — derivation needs the interface/type, not necessarily the full source.

Discussion & Comments

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