Difficulty: Easy
Correct Answer: Derived2 class
Explanation:
Introduction / Context:
This problem illustrates method hiding using the new modifier in C#. Unlike virtual/override dispatch, new creates a new member that hides the inherited one when the compile-time type matches the derived class.
Given Data / Assumptions:
Concept / Approach:
For non-virtual methods, the invoked method is selected based on the compile-time type of the reference. Because d has compile-time type Derived2, calling d.fun() invokes Derived2.fun(), printing “Derived2 class”. No base methods are called.
Step-by-Step Solution:
Verification / Alternative check:
Cast the reference to base types to observe different outputs: ((Derived1)d).fun() prints “Derived1 class” and ((Baseclass)d).fun() prints “Base class”.
Why Other Options Are Wrong:
They assume virtual dispatch or concatenated prints which do not occur without calling base methods explicitly.
Common Pitfalls:
Confusing new (hiding) with override (polymorphic override via virtual).
Final Answer:
Derived2 class
Derived
object if each int
is 4 bytes (ignore header/alignment)?
Discussion & Comments