Abstract classes contains both types of methods - abstract (non-implemented) as well as concrete (implemented) methods. While interfaces contain only non-implemented methods. It means interfaces are subset of abstract classes. Then why the interfaces was introduced in C# (.Net)? According to me, there are two reasons for it:
Is there any other reason or some hidden concept which I am missing?
What you're missing is considering the relationship between the two classes.
Inheritance (which you use with abstract classes) is an is-a
relationship. So if you were developing an application for a vet clinic you might create an Animal abstract class and then create Cat, Dog, Bird, and Fish from that, because Cat is-a
Animal, Dog is-a
Animal, etc.
Interface implementation defines a can-do
relationship. Perhaps you want to be able to Print several things in your application (Invoice, Animal, CustomerProfile). You shouldn't use inheritance (i.e. abstract class) for that because Invoice is-a
Print doesn't make any sense, however Invoice can-do
Print, CustomerProfile can-do
Print does make sense.
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