A Good Man
A Good Man

Reputation: 31

Can I have a 'protected' member in a parent class and inherit it in a child class?

I have 3 kinds of users like admin, customer, and supplier, which are all planned to be inherited from the User class. Can I use protected for username and password in the User class and inherit them in child classes? All tutorials seem to use private, is there any special reason for that?

Upvotes: 0

Views: 405

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063774

Can I use protected for username and password in user class

Yes

Whether you should is another question. If they are fields, they personally I'd say: keep the field private and add a protected property - or just simply:

protected string UserName {get;set;}

or maybe:

protected string UserName {get;private set;}

if only the base class should be able to set it, or

protected string UserName {get;}

if only the base class should be able to set it and only in the constructor

Side note: any discussion of storing a password - even in memory - makes me nervous.

Upvotes: 3

Related Questions