C# Interfaces – Abstract Class and Inheritance vs Interface

abstract classcinterfaces

Hello fellow programmers,

I am reading a book on C# and the author is comparing Abstract classes and Interfaces. He claims that if you have the following "abstract class:"

abstract class CloneableType
{
public abstract object Clone();
}

Then you cannot do this:

public class MiniVan : Car, CloneableType
{}

This, I understand. However he claims that because of this inability to do multiple inheritance that you should use an interface for CloneableType, like so:

public interface ICloneable
{
object Clone();
}

My question is, isn't this somewhat misleading, because you can create an abstract class which is "above" class Car with the method Clone, then have Car inherit that class and then Minivan will inherit Car with all these methods, CloneAble class -> Car class -> Minivan Class.

What do you think?
Thanks.

Best Answer

The difference between the abstract classes and interfaces in your case could allow the developer to well-define the pattern of your design with abstract class create "is a" relationship and interface create "can do" relationship.

Put it this way, if you have a Toy car and it would be not make sense to make them "is a" relationship with Minivan since you can use interface to exhibit toy car specific behaviour like water resistance tolerance.

So you can define like:

public Interface IToyCarResistance
{
  int ToleranceLevel { get; }
}

public class ToyCar : Car , IToyCarResistance
{
}