C# Programming - Inheritance - Discussion

Discussion Forum : Inheritance - General Questions (Q.No. 3)
3.
Which of the following statements are correct about the C#.NET code snippet given below?
namespace IndiabixConsoleApplication
{ 
    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(); 
        }
    }
}
  1. count should be declared as public if it is to become available in the inheritance chain.
  2. count should be declared as protected if it is to become available in the inheritance chain.
  3. While constructing an object referred to by i firstly constructor of index class will be called followed by constructor of index1 class.
  4. Constructor of index class does not get inherited in index1 class.
  5. count should be declared as Friend if it is to become available in the inheritance chain.
1, 2, 5
2, 3, 4
3, 5
4, 5
None of these
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
16 comments Page 1 of 2.

Amit Samnani said:   7 years ago
These answers are Totally wrong answer let me explain it in brief.

Answer B: 2, 3, 4

The second statement says, "count should be declared as protected if it is to become available in the inheritance chain"

Explanation: it is ok if it declares as Public.Public data member can also be inherited in the derived class.

Third option says , "While constructing an object referred to by i firstly constructor of index class will be called followed by constructor of index1 class."

Explanations: This answer seems to be ok!! but then it conflicts with answer option fourth.

Last option says, Constructor of index class does not get inherited in index1 class.

Explanations : totally wrong answer in the case of inheritance.

Shubham said:   2 months ago
@Mash.

The statement "Constructor of index class does not get inherited in index1 class" is correct because, in C#, constructors are not inherited by derived classes. Instead, each class has its constructor (s), and the constructors of the base class and derived class are called in a specific sequence during object instantiation.

John said:   9 years ago
@DKP - correct. If you create a solution and put a break point on the Count=0 line and run the solution, the break point gets hit.

So the option is incorrect. The constructor DOES get inherited.

CHANDRA SHEKHAR said:   1 decade ago
The subclass can inherit only the member variable and function not constructor if you want call the constructor of base class. You must have to create the object first of the base class.

DKP said:   9 years ago
Constructor of index class does not get inherited in index 1 class.

Is this option correct? It seems to be wrong. Constructor of index class will get inherited.

Umesh said:   10 years ago
In Inheritance Base class constructor is always executes first then only Child class constructor executes, if we are creating object of child class.

Rashmi said:   1 decade ago
When we declare as protected, it will availabe to subclass, it take intial value as zero for count variable, then it get increment.

DKP said:   9 years ago
@John.

Yes you are correct, if it is public it will work. But to inherit to child class it is enough to be protected.

Sneha said:   1 decade ago
How constructor of index class is not inherited in index1 class and still count variable is initialized to 0?

John said:   10 years ago
The answer is wrong. Even if it is declared public it is still available to child classes.


Post your comments here:

Your comments will be displayed after verification.