If we inherit a class do the private variables also get
inherited ?
Answers were Sorted based on User's Feedback
Answer / ashwini chavan
yes they are inherited, but cannot be accessible.because
when we inherite the class then all data member and function
member of the base class are accesed in the derive class
attomaticaly.but private specifier is accesible only it's
own class out side class can't acces that private member
when we try it then we got error
| Is This Answer Correct ? | 4 Yes | 0 No |
Answer / jigar
Yes, the variables are inherited but can not be accessed directly by the class interface.
| Is This Answer Correct ? | 4 Yes | 1 No |
Answer / 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 |
Answer / javed khan
No, We cant inherit private member of base class. We got
error like " member is inaccessible due to its protection
level."
| Is This Answer Correct ? | 7 Yes | 13 No |
What is the difference between writeline and write in c#?
Is c# a backend language?
What is difference between c sharp and c#?
What is ienumerator c#?
What is super class in c#?
Explain a MSIL ? Why is it appreciated by all developers?
Is hashset serializable c#?
What is typeof undefined?
what are the events in delegate?
You have got 1 million parking slots. At a time a parking slot can be free or not. To get next slot easily which data structure to implement?
Is c# strongly typed?
What is asp net in c#?
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)