If we inherit a class do the private variables also get
inherited ?

Answer Posted / sukriya

You can use private only with a nested class, one that is
defined within another class. The result is that the
private class is accessible only from within the containing
class
try this code
public class BaseClass
{
public virtual void TraceSelf()
{
Console.WriteLine("BaseClass");
Privateclass pc = new Privateclass();
pc.Self();
}

private class Privateclass
{
public void Self()
{
Console.WriteLine("PrivateClass");
}
}

}
public class SubClass : BaseClass
{
public override void TraceSelf()
{
//Privateclass pc = new Privateclass();
//pc.Self();
Console.WriteLine("SubClass");
}
}
static void Main(string[] args)
{
BaseClass obj = new BaseClass();
obj.TraceSelf(); // Outputs "BaseClass"

SubClass obj2 = new SubClass();
obj2.TraceSelf();

Console.ReadKey();
}
this is the main Program for the code

Is This Answer Correct ?    2 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the use of console readkey ()?

738


What is the difference between an integer and int?

664


What is inline function in c#?

706


Why var is used in c#?

712


What is the difference between string and stringbuilder in c#?

685


Why we use dll in c#?

709


Explain the importance and use of each, version, culture and publickeytoken for an assembly.

694


What is xml c#?

658


Define thread?

753


Which is executed if an exception has not occurred?

690


What does console mean c#?

681


What is the difference between virtual and override in c#?

718


Can an array be null c#?

705


How can you reference current thread of the method ?

706


What does the parsefloat function do?

666