C# Programming - Inheritance - Discussion

Discussion :: Inheritance - General Questions (Q.No.2)

2. 

Which of the following statements should be added to the subroutine fun( ) if the C#.NET code snippet given below is to output 9 13?

class BaseClass
{
    protected int i = 13;
}
class Derived: BaseClass
{
    int i = 9; 
    public void fun()
    {
        // [*** Add statement here ***]
    } 
}

[A]. Console.WriteLine(base.i + " " + i);
[B]. Console.WriteLine(i + " " + base.i);
[C]. Console.WriteLine(mybase.i + " " + i);
[D]. Console.WriteLine(i + " " + mybase.i);
[E]. Console.WriteLine(i + " " + this.i);

Answer: Option B

Explanation:

No answer description available for this question.

Somu said: (Sep 1, 2011)  
If I remove protected then it show error. Why?

Ravi said: (Dec 1, 2011)  
Because on removing protected keyword int I will become public by default, and now for derived there would be two i variable.

Neelendu said: (Dec 13, 2012)  
Removing protected keyword become private. Because default scope of class member is private which is not accessible by other class or type.

Here use of protected is:

Only child class access this data.

Kumar said: (Apr 4, 2013)  
Removing protected keyword means these variable private data type. So this variable use only base class, does not inherited child class.

Sudhakar said: (Jun 10, 2013)  
Private is the default access modifier for members, if you don't specify access specifier then compiler assumes it is private, private members are not accessible in derived class hence you will get error.

Chandra Shekhar said: (Aug 30, 2013)  
If we remove protected modifier by default it become private which is not accessible in child class.

Divya said: (Dec 13, 2013)  
By default csharp class members are private when the access specifier is absent. So, it can't access child class features (derived class features).

Ajesh said: (Dec 25, 2013)  
What is base.i?

Govind said: (Aug 5, 2014)  
Where is create object name base ?

It is directly used it.

Jana said: (Aug 13, 2014)  
To get output as 9 13,

First you need to get the derived class i value and then you can get the base class i value by object of base class as base.i

Where base is object of BaseClass.

Pavan said: (Dec 15, 2014)  
What is the difference between base.i and mybase.i?

Amit said: (Jan 15, 2015)  
using System.IO;
using System;

public class BaseClass
{
public int k = 13;
}
public class Derived: BaseClass
{
int i = 9;
public static void Main()
{
Derived d=new Derived();
Console.WriteLine("{0} {1}",d.i,d.k);
}
}

Mash said: (Aug 16, 2015)  
@Pavan.

Base is a keyword of c# which is used here to call the I variable of the base class-BaseClass to differ it from the I variable of the derived class-Derived. Whatever, actually it was not necessary to do this if both of the variable were not declared using the same identifier-i.

Karthi said: (Feb 9, 2016)  
Hi,

Both option A and B are same. May I know what is the difference between them?

Ndikho said: (Apr 28, 2016)  
@Karthi.

They might look the same but please check the format should the output(9 13) be.

In case, you not aware, 'base' refers to the base class/superclass. So if you say 'base.i' you refer to base class.

Hope you answered.

Sushil said: (Feb 1, 2017)  
But we can't use base keyword under a static block of a derived class.

Sandeep said: (Dec 13, 2017)  
In BaseClass 'i' is local variable, and inside DerivedClass 'i' is again a local variable of it, So, 9 value scope is inside the DerivedClass and 13 scope is outside the DerivedClass, so, to call the base class members we should use "base" keyword console.WriteLine(i+""+base.i);.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.