thekonger
thekonger

Reputation: 179

c# Inherit from derived class or use an interface

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; }
}

Upvotes: 0

Views: 113

Answers (3)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64933

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.

Upvotes: 1

Arun Ghosh
Arun Ghosh

Reputation: 7734

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.

Upvotes: 0

Kendall Frey
Kendall Frey

Reputation: 44316

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.

Upvotes: 3

Related Questions