0

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; }
}
1
  • This really depends on how you plan to use the class. Commented Oct 1, 2014 at 15:08

3 Answers 3

3

It's perfectly legal, and can be quite useful. Look at the inheritance tree for Button:

If you need to provide common functionality between classes, a base class is often the best approach.

Sign up to request clarification or add additional context in comments.

Comments

1

In c# is it possible to inherit from a derived class?

If you dig a little on what's C# and its features you'll find that C# is a fully-object-oriented-programming language, thus the answer is obvious: yes.

In the other hand, serious programming languages with serious compilers won't let you do ilegal things. If you can compile it, it's valid from the point of view of syntax (it might crash during run-time, but a storng-typed compiled language wouldn't allow you to derive a class in design-time and crash during run-time...).

About interfaces...

An interface isn't suitable to simulate inheritance. They're contracts and their use case is ensuring that implementers will need to provide an implementation to interface members. Furthermore, interfaces support inheritance, but it's for the sake of creating wider contracts based on other ones.

2 Comments

Nice detail. I like your explenantion about 'Inheritance' in interfaces
Matias, wish I could give you a point too on your compiler explanation, but doesn't seem like I can. Looks like you echoed my understanding of an interface. I can see now it was obvious based upon the example Kendall gave. I googled 'inherit from derived class' though and couldn't find an explicit example. Probably was right in front of me.
0

You have another choice too ~ make class A and B abstract. If you don't want to have instances of A and B, make it abstract or interface.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.