C#.NET inheritance and accessibility — analyze the following program and choose the valid statements. namespace CuriousTabConsoleApplication { class index { protected int count; public index() { count = 0; } } class index1 : index { public void increment() { count = count + 1; } } class MyProgram { static void Main(string[] args) { index1 i = new index1(); i.increment(); } } } Which statements are correct regarding accessibility and constructor behavior in this inheritance setup?

Difficulty: Easy

Correct Answer: 2, 3, 4

Explanation:


Introduction / Context:
This question checks your grasp of C#.NET inheritance basics: how access modifiers (public, protected) behave across a base–derived chain, and how constructors run when an instance of a derived class is created.



Given Data / Assumptions:

  • count is declared in the base class index.
  • index1 derives from index and writes to count in increment().
  • Normal C# constructor invocation order applies (base before derived).


Concept / Approach:
Members marked protected are accessible in the declaring class and in derived classes. Public is broader (everywhere), but not required for inheritance visibility. Constructors are not inherited as members; they are invoked in a chain: base constructor executes first, then the derived constructor.



Step-by-Step Solution:

Statement 1: “count should be public to be available” → False. protected is designed for inheritance access; public is not necessary.Statement 2: “count should be protected to be available” → True. Matches C# rules.Statement 3: “While constructing, base constructor is called first, then derived” → True. Construction flows base → derived.Statement 4: “Constructors do not get inherited” → True. Constructors are not inherited; each class defines its own.Statement 5: “Friend (VB) should be used” → False in C#. The nearest term is internal, which is assembly visibility, not inheritance-specific.


Verification / Alternative check:
Make count private and see the compile error in index1.increment(). Switch back to protected to fix it. Add a diagnostic print in base and derived constructors to observe the base-first, derived-second order.



Why Other Options Are Wrong:
Any option including 1 or 5 is wrong because they mis-state the correct access level for inheritance or use a non-C# access modifier concept.



Common Pitfalls:
Confusing public (world-visible) with protected (inheritance-visible). Assuming constructors are inherited like methods (they are not).



Final Answer:
2, 3, 4

More Questions from Inheritance

Discussion & Comments

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