Call parent method from child class c
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C sharp inheritance, a child class can call parent behavior with the base keyword. The syntax is simple, but design choices around call order and override policy determine whether your class hierarchy stays reliable. A good implementation treats parent calls as contract decisions, not just syntax tricks.
Basic Parent Call with base
A child class overrides a virtual method and invokes parent implementation using base.MethodName(...). This is the standard extension pattern when parent logic must still run.
This preserves parent validation and adds child-specific behavior.
Call Order Matters
Parent-first and parent-last are not equivalent. The right order depends on invariant ownership.
Use parent-first when base method enforces validation, setup, or authorization preconditions. Use parent-last when base method emits finalization behavior such as audit or cleanup that must include child changes.
If order is important, document it in code comments or method contract docs. Otherwise, future overrides may accidentally break lifecycle expectations.
Constructor Chaining Is Parent Invocation Too
Calling parent from child also applies to constructors with : base(...). This is required when parent has no default constructor or needs mandatory arguments.
If parent constructor work is heavy, make side effects explicit so object creation remains predictable.
Template Method Pattern for Safer Extension
A robust inheritance style avoids relying on child authors to remember base calls. The parent can expose a non-virtual workflow method that calls protected virtual hooks.
This pattern guarantees base invariants always execute.
override Versus new
A common mistake is method hiding with new when true polymorphic override was intended. Hidden methods do not participate in virtual dispatch through base references.
Use override unless you intentionally need hiding for compatibility reasons. If hiding is intentional, document it clearly because behavior can surprise maintainers.
Testing Parent-Child Contracts
Inheritance errors often appear as behavior drift rather than compile failures. Add tests for:
- base validation is preserved
- call order is correct
- exceptions propagate as expected
- side effects happen exactly once
These tests are especially important before refactoring class hierarchies.
Common Pitfalls
Forgetting to call base when base method enforces required validation breaks invariants silently.
Calling base in the wrong order can produce stale state or incomplete logging.
Using new instead of override causes confusing runtime behavior through parent references.
Putting hidden side effects in parent constructors makes child instantiation harder to reason about.
Summary
- Use
baseto call parent behavior from child overrides and constructors. - Decide base-first or base-last based on invariant ownership.
- Prefer template-method style when base steps must always run.
- Avoid accidental method hiding when polymorphism is required.
- Protect parent-child contracts with behavior-focused tests.

