hi friends
Are private class-level variables inherited?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Priya LingePosted Nov 14, 2011, 11:35 PM
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 MorePosted Nov 14, 2011, 11:40 PM
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.