Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I decide whether to use an interface or an abstract class?

Tags:

c#

oop

I have been into C# and always feel confusion between what should be selected between interfaces and abstract classes. Can some one please help resolve this query ?

Thanks ,

like image 388
Anil Namde Avatar asked Jul 02 '10 15:07

Anil Namde


People also ask

How would you decide whether to use an interface or abstract 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.

What is advantage of using interfaces over abstract classes?

The main advantages of interface over abstract class is to overcome the occurrence of diamond problem and achieve multiple inheritance. In java there is no solution provided for diamond problem using classes. For this reason multiple inheritance is block using classes in java.

Are interfaces preferred over abstract classes?

Anyway, it depends on what you are trying to create. Abstract classes are preferred if the subclasses share some common functionalities with the superclass. Interfaces when you create unrelated classes to implement the interface. Interfaces allow you to implement multiple inheritances.

When would you use an interface?

Uses of interface: To define a contract. To link unrelated classes with has a capabilities (e.g. classes implementing Serializable interface may or may not have any relation between them except implementing that interface. To provide interchangeable implementation e.g. Strategy_pattern.


4 Answers

Think of an interface like a contract, you are specifying something that you are expecting the consumers of that interface to implement.

An abstract class, on the other hand, is useful when you have some of the code you need to implement for the class, but not all of it. And you can declare abstract methods for the parts that need to be implemented by the subclasses of the abstract class. Remember that abstract methods must be implemented by the subclasses, but you can also provide your own code within the class itself via normal private/public/protected/etc. methods.

So if you are just writing a contract that you want subclasses to implement, then think interface. But if you are writing something that's more of a "pattern", where you might have some of the method implementations (but not all) that will be common to all the child implementations, then you should think abstract class.

like image 80
dcp Avatar answered Sep 20 '22 11:09

dcp


Neither is "better"--they have different purposes.

  • An interface is for when you need to define a set of common methods with similar semantics, but the classes that define those methods can't inherit from the same source, and might have completely different implementations.

  • An abstract class is for when you want to partially implement certain functionality, but delegate important parts to a subclass. The implementation of an abstract class is much more restricted than the implementation of an interface, since the implementation classes have to inherit from the abstract class, and they cannot override the parts of the base class that aren't virtual or abstract.

like image 27
JSBձոգչ Avatar answered Sep 22 '22 11:09

JSBձոգչ


It depends on what you're trying to do. Abstract classes are good when you have common functionality with common state. Even if you use an abstract class, it's often helpful to provide an interface as well, since you don't always need access to the state and other classes could potentially implement the interface without that state (such as a test stub.)

If you only need helper methods (no state, simply composing method calls), then I prefer to use an interface with extension methods for the helper functions (such as overloads).

like image 39
Dan Bryant Avatar answered Sep 23 '22 11:09

Dan Bryant


There are already some code answers, but I want to add another perspective that a lot of developers forget about.

If you use an interface in a public API then you have committed yourself to those members it contains and only those members. If you attempt to add something to an interface then it is a version breaking change. Why? Because every class must implement every member in an interface. Code that uses your API will stop compiling.

Abstract classes do not suffer this same limitation since you can add methods and provide a reasonable default implementation for them. Subclasses are then none the wiser and would not be impacted by the change.

like image 41
Brian Gideon Avatar answered Sep 22 '22 11:09

Brian Gideon