C# Programming - Inheritance - Discussion
Discussion Forum : 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 ***]
}
}
Discussion:
17 comments Page 2 of 2.
Pavan said:
1 decade ago
What is the difference between base.i and mybase.i?
Amit said:
1 decade ago
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);
}
}
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:
1 decade ago
@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.
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:
10 years ago
Hi,
Both option A and B are same. May I know what is the difference between them?
Both option A and B are same. May I know what is the difference between them?
Ndikho said:
9 years ago
@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.
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:
9 years ago
But we can't use base keyword under a static block of a derived class.
Sandeep said:
8 years ago
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers