In c# is it possible to inherit from a derived class? For example I have the following structure, is it legal in C# to do? It compiles but I am wondering if this will cause issues later and throw an error in certain conditions.
public class A
{
public string s1 { get; set; }
public string s2 { get; set; }
}
public class B : A
{
public string s3 { get; set; }
public string s4 { get; set; }
}
public class C : B
{
public C()
{
this.S1 = "A prop";
this.S2 = "A prop";
this.S3 = "B prop";
this.S4 = "B prop";
}
public string s5 { get; set; }
public string s6 { get; set; }
}
I could also use an interface but from my understanding an interface just enforces a contract on anything that implements it. So I would still need to implement all the properties in the derived class. I guess that's not much more code than this, so perhaps this is the proper way to do this? In this case, I think C implementing IA and IB just means C must implement the properties s1, s2, s3, and s4. Is that correct? And if so, is this the better way of doing this? And if I am off in both scenarios let me know. Thanks
interface IA
{
string s1 { get; set; }
string s2 { get; set; }
}
interface IB : IA
{
string s3 { get; set; }
string s4 { get; set; }
}
public class C : IA, IB
{
public C()
{
this.S1 = "A prop";
this.S2 = "A prop";
this.S3 = "B prop";
this.S4 = "B prop";
}
public string s1 { get; set; }
public string s2 { get; set; }
public string s3 { get; set; }
public string s4 { get; set; }
public string s5 { get; set; }
public string s6 { get; set; }
}
