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
What is the main method in c#?
How do I use the 'using' keyword with multiple objects?
What is toint32 c#?
What is the process of delegation?
How does inheritance work in c#?
what are the different ways a method can be overloaded?
How do I link two windows forms in c#?
List out the differences between array and arraylist in c#?
What are value types and reference types?
What does async mean in c#?
How to reverse each word in a string using c#?
Tell me the difference between call by value and call by reference.
What is the use of oops in c#?
Explain the difference between a Private Assembly and a Shared Assembly
What are the 2 broad classifications of fields in c#?