C# Programming - Inheritance - Discussion

Discussion Forum : Inheritance - General Questions (Q.No. 15)
15.
Which of the following statements is correct about the C#.NET program given below?
namespace IndiabixConsoleApplication
{
    class Baseclass
    { 
        int i;
        public Baseclass(int ii)
        {
            i = ii;
            Console.Write("Base "); 
        } 
    } 
    class Derived : Baseclass
    {
        public Derived(int ii) : base(ii)
        {
            Console.Write("Derived ");
        } 
    } 
    class MyProgram
    { 
        static void Main(string[ ] args)
        { 
            Derived d = new Derived(10);
        } 
    } 
}
The program will work correctly only if we implement zero-argument constructors in Baseclass as well as Derived class.
The program will output: Derived Base
The program will report an error in the statement base(ii).
The program will work correctly if we replace base(ii) with base.Baseclass(ii).
The program will output: Base Derived
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
8 comments Page 1 of 1.

RSJ said:   1 year ago
The Derived class inherits from the Baseclass. In the constructor of the Derived class, it calls the base class constructor using base (ii) , where ii is passed from the Derived constructor. The base class constructor initializes the variable I with the value passed to it and then prints “Base”.

After initializing the base class, control returns to the derived class constructor, which then prints “Derived”.

Therefore, when you run this program, the output will be: Base Derived.

Mayur said:   9 years ago
The constructor of the derived class implicitly calls the constructor of the base class.

Ramaiah said:   9 years ago
Actually, the base class object is created in the program that's why the base class method is invoked. If you create derived class object with derived class reference then only we can call both class methods.

Harish said:   1 decade ago
Base keyword is used to call the base class constructor.

AhmedIsmail said:   1 decade ago
Why E ? Can the constructor be inherited ?

Kabi said:   1 decade ago
Can anybody tell me how it's giving the option E Answer?

Because when we use the parameterise type of constructor we need to pass the default constructor.

Ayush Sethi said:   1 decade ago
The base keyword used in derived class gives the call to its base class.

Tejas said:   1 decade ago
Constructor call it self when we create object of it.

Post your comments here:

Your comments will be displayed after verification.