C# Programming - Inheritance - Discussion

Discussion :: 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.

[A]. 1, 2, 5
[B]. 2, 3, 4
[C]. 3, 5
[D]. 4, 5
[E]. None of these

Answer: Option B

Explanation:

No answer description available for this question.

Arun Saraswat said: (Aug 26, 2011)  
How inheritance work? and in this case how we are going to be inherit the function ?

Rashmi said: (Oct 3, 2011)  
When we declare as protected, it will availabe to subclass, it take intial value as zero for count variable, then it get increment.

Siddesh said: (May 28, 2013)  
Even if we declare it public it is available to the sub classes.

Sneha said: (Aug 22, 2013)  
How constructor of index class is not inherited in index1 class and still count variable is initialized to 0?

Chandra Shekhar said: (Aug 30, 2013)  
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.

Radhika said: (Sep 5, 2014)  
How constructor to index class?

Umesh said: (Sep 25, 2014)  
In Inheritance Base class constructor is always executes first then only Child class constructor executes, if we are creating object of child class.

John said: (Feb 3, 2015)  
The answer is wrong. Even if it is declared public it is still available to child classes.

Dkp said: (Mar 31, 2015)  
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.

Dkp said: (Mar 31, 2015)  
@John.

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

John said: (May 9, 2015)  
@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.

Mash said: (Aug 16, 2015)  
Answer seems all right to me.

Amr said: (May 16, 2017)  
I do not understand the third option. Please explain it for me.

Amit Samnani said: (Nov 14, 2017)  
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.

Anju said: (May 19, 2018)  
Of course, the base class constructor will be called first.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.