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
Explain the differences between static, void and public in c#?
When should we use sealed class in c#?
What is string class in c#?
Can we write class inside a class in c#?
What is yield return in c#?
How to find the current application file path while runtime?
What is difference between dictionary and hashtable in c#?
What is the difference between C# 3.5 and C# 4.0?
What is arraylist class in c#?
What is a interface in c#?
Can scriptable objects have methods?
How does inheritance work in c#?
How to handle exceptions that are raised in a component?
In a memory when you Box and Unbox a value-type what happens?
what will be the output of the given below coding. using System; public class Exercise { static void OddNumbers(int a) { if (a >= 1) { Console.Write("{0}, ", a); a -= 2; OddNumbers(a); } } public static int Main() { const int Number = 9; Console.WriteLine("Odd Numbers"); OddNumbers(Number); Console.WriteLine(); return 0; } }