hi friends
Are private class-level variables inherited?
Private class variable
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Priya LingeRank #785 · 2.9K reputationPosted Nov 14, 2011, 6:35 PM EST
1.They are inherited, but the keyword "private" means that only the class in which they were declared can access them.
2.If you want the subclass to access the base class variable, use protected instead of private.
3.They are not directly accessible.
4.Example :
public class BaseClass
{
private const int _aNumber = 5;
public int ReturnANumber() { return _aNumber; }
}
Public class Dclass :BaseClass
{
public void Method()
{
// This is indirectly accessing a private implemented in the class
int numbery = this.ReturnANumber();
}
}
Hope this will help you.
Thanks.
Pravin MoreRank #1,419 · 1.2K reputationPosted Nov 14, 2011, 6:40 PM EST
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
Private fields are inherited in the sense, that they take up space on the heap when allocated. However, the derived class cannot access them directly.
Thanks,
Pravin.
Mark "Accepted answer" if this post help you.