3

Lets say there is an interface IRun:

public interface IRun
{
    public bool IsRunComplete();    
}

Instead of inheriting from this interface directly , the author inherited the methods of this interface to a class , and inherited from this class wherever it is required.

Public Class RUN : IRun
{
    public abstract bool IsRunComplete()
}

What is the benefit one would get by doing this way instead of directly inheriting from the interface.?

This question is not about interfaces Vs abstract, rather it is on understanding the reason behind implementing interfaces in this method.

harin04
  • 271
  • 5
  • 16
  • You question boils down to `Why use an abstract class?`. See here: http://stackoverflow.com/a/761342/701062 – Gary.S Feb 26 '15 at 05:12
  • 1
    Unless the RUN class introduces (declares or implements) something else, I can see no benefit. It is possible the author had in mind to add something else into RUN in the future, and started it this way to avoid a future refactor. – Zenilogix Feb 26 '15 at 05:13

2 Answers2

2

If the Run class really does only have a single abstract method then there is no real need for that class. Sometimes when you see code and you cannot work out the purpose, it is because the original design or idea was a poor one.

Phil Wright
  • 22,580
  • 14
  • 83
  • 137
2

This is a common practice in c#. Here is such a scenario that this pattern may help.

  • When you wanted to do a refactoring or re-implementing on existing class. To avoid broken the existing code, you first introduce an interface, and then change code reference to use that interface instead of the original class, and use a factory method to create the class instance instead of access the constructor. Finally, you are free to replace the class with a brand new one - just let the factory method return instances of the new class. When you create the new one, you may want it to share some code with the old one, so letting the new class share a same base of the old one and move the shared code to the base class is good way to maintain compatibility.
Earth Engine
  • 10,048
  • 5
  • 48
  • 78