Run-time polymorphism (method overriding) in C#: which statements are required/true?

Difficulty: Easy

Correct Answer: 1, 2, and 5

Explanation:


Introduction / Context:
Run-time polymorphism in C# occurs when a base-class reference invokes an overridden method on a derived object. Understanding the rules for 'virtual', 'override', and accessibility ensures correct behavior and avoids compiler errors.



Given Data / Assumptions:
Statements (paraphrased):

  • 1) The overridden base method must be virtual, abstract, or already override.
  • 2) The override method must have the same access level as the base virtual method.
  • 3) An override declaration can change the accessibility of the virtual method.
  • 4) An abstract inherited property cannot be overridden in a derived class.
  • 5) An abstract method is implicitly virtual.


Concept / Approach:
C# requires base methods to be marked virtual/abstract/override to be overridden. Overridden members must keep the same accessibility, and abstract methods (and properties) are implicitly virtual and must be overridden (unless the derived class is abstract too).



Step-by-Step Solution:

Statement 1: True. Without virtual/abstract/override in the base, the derived cannot override.Statement 2: True. Accessibility cannot be changed (e.g., protected virtual cannot become public override).Statement 3: False. Overriding cannot widen/narrow accessibility.Statement 4: False. Abstract properties are intended to be overridden.Statement 5: True. Abstract implies virtual.


Verification / Alternative check:
Create a protected virtual method in the base and attempt to override as public; the compiler errors on accessibility change. Override an abstract property; compilation is required unless the derived class remains abstract.



Why Other Options Are Wrong:
Options including 3 or 4 contradict C# rules; only 1, 2, and 5 stand together correctly.



Common Pitfalls:
Confusing 'new' (method hiding) with 'override' (polymorphism), and forgetting abstract members are implicitly virtual.



Final Answer:
1, 2, and 5

Discussion & Comments

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