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();
}
}
}
- count should be declared as public if it is to become available in the inheritance chain.
- count should be declared as protected if it is to become available in the inheritance chain.
- While constructing an object referred to by i firstly constructor of index class will be called followed by constructor of index1 class.
- Constructor of index class does not get inherited in index1 class.
- count should be declared as Friend if it is to become available in the inheritance chain.
Discussion:
16 comments Page 1 of 2.
Amr said:
7 years ago
I do not understand the third option. Please explain it for me.
(1)
Arun saraswat said:
1 decade ago
How inheritance work? and in this case how we are going to be inherit the function ?
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.
Siddesh said:
1 decade ago
Even if we declare it public it is available to the sub classes.
Sneha said:
1 decade ago
How constructor of index class is not inherited in index1 class and still count variable is initialized to 0?
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.
Radhika said:
1 decade ago
How constructor to index class?
Umesh said:
1 decade ago
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:
10 years ago
The answer is wrong. Even if it is declared public it is still available to child classes.
DKP said:
10 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.
Is this option correct? It seems to be wrong. Constructor of index class will get inherited.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers