Suppose we have two methods M1()
and M2()
in an interface. An abstract class also has just the same two abstract methods. If any class implemented this interface or inherited from the abstract class, it will have to implement both the methods in it.
So to me, it seems that an interface or an abstract class behaves the same for my scenario. So, can anyone highlight the difference between these two in this specific case and suggest whether to use an abstract class or an interface here?
Interface: Explore the Difference between Abstract Class and Interface in Java. The Abstract class and Interface both are used to have abstraction. An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class.
If you are creating functionality that will be useful across a wide range of objects, then you must use an interface. Abstract classes, at the end of the day, should be used for objects that are closely related. But the interfaces are best suited for providing common functionality to unrelated cases.
In C#, an Interface provides only those public services declared in the interface, whereas an abstract class provides the public services defined in an abstract class and those members that are inherited from the abstract class's base class.
To answer your question, yes you could use an abstract class (providing no implementation) instead of an interface but I'd consider this bad practice: You've used up your "one-shot" at inheritance (without gaining any benefit). You cannot inherit from multiple abstract classes but you can implement multiple interfaces.
There are technical differences between Abstract Classes and Interfaces, that being an Abstract Class can contain implementation of methods, fields, constructors, etc, while an Interface only contains method and property prototypes. A class can implement multiple interfaces, but it can only inherit one class (abstract or otherwise).
However, in my opinion, the most important difference between Interfaces and Abstract Classes is the semantic difference.
An Interface defines what something can do (how it behaves), and an Abstract Class defines what something is.
Take for example IEnumerable
, the semantic meaning behind this is that anything that implements IEnumerable
is enumerable, it doesn't mean that it's an enumeration, but that it can behave like one (can be enumerated).
Contrast that with a washing machine example, anything that inherits it is a type of washing machine. Anything that inherits it would be a type of washing machine, a top loader, or side loader, etc.
Instead, if you had an interface called ICanWash
, which could contain a method called Wash
. You could have various things implement ICanWash
, be it a Person
, an abstract washing machine class, etc, where the actual implementation does not matter, just you need to know that the behavior is that it can wash things.
In summary, classes define what something is, interfaces define what something can do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With