C # Access Modifiers with Inheritance

I would like to have several versions of an object with different access modifiers in properties

For example, I might have a user class -

public abstract class UserInfo { internal UserInfo() { } public virtual int ID { get; set; } public virtual string Password { internal get; set; } public virtual string Username { get; set; } } public class ReadUserInfo : UserInfo { internal ReadUserInfo() { } override public int ID { get; internal set; } override internal string Password { get; set; } override public string Username { get; internal set; } } public class NewUserInfo : UserInfo { internal NewUserInfo() { ID = -1; } //You get the Idea } 

Can I implement this, or do I need to control access in a more programmatic way?

+4
source share
2 answers

you can use the new modifier:

 public class ReadUserInfo : UserInfo { internal ReadUserInfo() { } new public int ID { get; internal set; } new internal string Password { get; set; } new public string Username { get; internal set; } } 
+4
source

Is inheritance really suitable? Users of the UserInfo class should not be aware of the subtypes. In this case, users should be aware that the Password property is not available if the ReadUserInfo instance is ReadUserInfo , and not the UserInfo instance.

It really doesn't make sense.

Edit: in OO design, this is called the Liskov replacement principle.

+14
source

All Articles