Should I be using inheritance or interface?

What's the difference between:

type IFooable =
    interface IDisposable

    abstract Foo : (unit -> unit)

      

and

type IFooable =
    inherit IDisposable

    abstract Foo : (unit -> unit)

      

?

If equivalent, in what cases should I use one over the other? Thank!

+2


source to share


3 answers


(I originally thought that) You should use 'inherit' with (at most one) base class. You can use an "interface" for whatever interfaces you want to implement.

Thus, in the case of IDisposable, it must be an "interface".



EDIT, it's ok that the compiler allows this, but it might be a bug, I'll take a look

EDIT: it might turn out to be a likely error in a different way, and probably interfaces will force you to use "inherit" to inherit other interfaces, the idea is that the "inherited" members are always visible directly in the sense of "implicit interface", whereas the declaration An "interface" in a class is an "explicit" interface that requires the use of that interface type in order to use these elements. Either way, we'll probably remove this flexibility in the syntax of the language, so there is only one way the author of this, not two equivalent ways.

+6


source


I usually use inheritance for true oop (i.e. X isa Y) and interfaces as a kind of decorator implementation.

probably not a de facto "correct" way to do it, but we really like it.

I mean X inherits Y, but then we want to give it additional functionality from V, W and Z.



"Jetta isa Car, but also IDiesel, IGerman, IBroken"

this is just how my current project is doing it and I will probably get my head around "oop abuse" or something :)

0


source


u must use interfaces if you are going to use abstract methods. Since you can only inherit from one class, but you can implement many interfaces.

-1


source







All Articles