Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance design using Interface + abstract class. Good practice?

I'm not really sure how to title this question but basically I have an interface like this:

public interface IFoo
{
    string ToCMD();
}

a couple of absract classes which implement IFoo like:

public abstract class Foo : IFoo
{
   public abstract string ToCMD();
}

public abstract class Bar : IFoo
{
    public abstract string ToCMD();
}

then classes which inherit Foo and Bar:

public class FooClass1 : Foo
{
    public override string ToCMD()
    {return "Test";}
} ///there are about 10 foo classes.

public class BarClass : Bar
{
    public override string ToCMD()
    {return "BarClass";}
} ///about the same for bar classes.

I am doing this so that when I have my custom list like:

public class Store<T> : List<T>  where T : IFoo {}

I then can restrict the types that go in it but by having the interface it will still take any type of IFoo.

Something like:

Store<Foo> store = new Store<Foo>(); //Only Foo types will work.
store.Add(new FooClass1()); //Will compile.

Store<IFoo> store = new Store<IFoo>(); //All IFoo types will work.
store.Add(new FooClass1()); //Will compile.
store.Add(new BarClass()); //Will compile.

My question is: Is this an ok way of going about this? or is there a better way?

EDIT: Picture-> alt text

like image 659
Nathan W Avatar asked Dec 18 '08 01:12

Nathan W


People also ask

Can you inherit from an abstract class and an interface?

An abstract class defines the identity of a class. An interface can inherit multiple interfaces but cannot inherit a class. An abstract class can inherit a class and multiple interfaces.

Can we use inheritance in abstract class?

An abstract class cannot be inherited by structures. It can contain constructors or destructors. It can implement functions with non-Abstract methods. It cannot support multiple inheritances.

Why might an abstract class be useful in the inheritance hierarchy?

An abstract class is a good choice if we are using the inheritance concept since it provides a common base class implementation to derived classes. An abstract class is also good if we want to declare non-public members. In an interface, all methods must be public.

Which is better inheritance for interfaces?

We can inherit lesser classes than Interface if we use Inheritance. We can inherit enormously more classes than Inheritance, if we use Interface. Methods can be defined inside the class in case of Inheritance.


1 Answers

The need for an inheritance chain is questionable, in general.

However the specific scenario of combining an abstract base class with an interface.. I see it this way:

If you have an abstract base class like this, you should also have a corresponding interface. If you have an interface, then use the abstract base class only where the inheritance chain is sensible.

That said, if I'm writing a library and this is part of my API/Framework, I will generally include a "default implementation" that can be used as a base class. It will implement the interface methods in a naive, general way, where possible, and leave the rest for inheritors to implement/override as needed.

This is just a convenience feature of the library though, to assist people who want to implement the interface by providing a functional example that may cover most of what they need to implement already.

In short, the interface is more valuable than the base class, but a base class might save a lot of time and reduce buggy interface implementations.

like image 124
Troy Howard Avatar answered Sep 18 '22 20:09

Troy Howard