-1

what is the main utility of Interface. we know that we can implement dynamic behaviour using interface but i guess it is not only the utility. so i like to know when we have to write interface and when we need to go for abstract class.

show me 5 or 10 most important uses of interface in real life scenario.

another main use is coming to my mind that project manager or team lead will implement basic skeleton through interface and other developer follow it.

so please guys show me with sample code few most important use of interface which we can do with abstract class or concrete class.

one guy told me like this way which is not very clear to me

interfaces are defined contracts between classes or structs, consumers can exchange the implementation by a different one as long as the same contract is met that is the method names and signature that compose a specification that classes and structs can work against rather than working against a concrete implementation.

The important part about interfaces is to know when to use them and as a matter of fact it's quite simple, when you want two or more unrelated objects to have the same common functionality but not necessarily the same implementation you will want to use interfaces; otherwise, when you have related objects that have a shared functionality and implementation then you may consider to use an abstract class instead of an interface.

this thing is not clear specially

when you want two or more unrelated objects to have the same common functionality but not necessarily the same implementation you will want to use interfaces; otherwise, when you have related objects that have a shared functionality and implementation then you may consider to use an abstract class instead of an interface.

it would be nice if anyone explains with sample code when to go for interface & when abstract class. show me few best important area which is always handle with interface with sample code or best interface uses with sample code.thanks

dcastro
  • 66,540
  • 21
  • 145
  • 155
Thomas
  • 33,544
  • 126
  • 357
  • 626
  • Let's go with an interface you are using every day: IEnumerable. It gives objects the opportunity to be iterated over. It is automatically used in foreach() loops. – ASA Feb 20 '14 at 10:29

3 Answers3

1

I won't answer all you questions. I just want to give you some hints.

  1. The main difference between an interface and an abstract class is, that a c# class can implement multiple interfaces even if they declare the same members. And it can even implement those equally named members differently by implementing the interface explicitly.

  2. If you derive from an abstract class, you also "inherit" al its dependencies. For example if a method in an abstract class uses another class from a different assembly, you have to reference that assembly. --> Compile order --> No parallel build

  3. Mocking in unittest can be trickier when using abstract classes with base functionality

Scordo
  • 1,041
  • 7
  • 12
  • Your second point applies to interfaces too. Interfaces can have dependencies of course, parameter types or return types, or inherited interfaces. – Kris Vandermotten Feb 20 '14 at 10:33
  • This can be the case, but in a well structured project those dependencies (Container-Classes and so on) reside in the same Contract-Assembly. – Scordo Feb 21 '14 at 12:32
1

Some of microsoft recommendation from this link

  1. If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
  2. If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
  3. If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
  4. If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
Milan Raval
  • 1,880
  • 1
  • 16
  • 33
1

Let's take for instance some Data Access Objects which can retrieve data from a DB, a SAOP Service, a REST Service or even an XML file.

You would use Interfaces to ensure what kind of operations they offer to the rest of the application. You can also say that those interfaces describe the Domain and how they interact with it.

public interface IUserDao
{
    User GetUserById(int id);
    void AddUser(User u);
    ....
}

This IUserDao can be implemented by using WCF, Entity Framework, XmlDocuments, and many other techniques, the controller or other parts of the application don't care about the details as long as they have those abstracted methods to retrieve and add a user.

On the other hand the same Data Access Objects can have a base class which can for instance initialize some connections or open the XmlDocument, ...

public abstract BaseDao
{
    public Connection GetNewConnection()
    {
        ....
    }

    // or similar functions which are used by DAOs accessing the same data source (DB, XML, ...)
}

So as it was described, you can use interfaces to hide implementation details and bring the implementation to a more absract level, this way, less skilled developers or developers more interested in the domain specific aspects (some specific calculation, ...) can contribute without the need to understand how exactly they need to retrieve and store the data from / to the database.

Also it is easier to exchange functionality, for instance you can start with a simple xml file but soon you'll realize that you'll need a whole DB - you can keep the interfaces and implement the classes with DB access.

On the other hand abstract classes share basic functionality (technical functionality), which is so basic that it is used by many classes but shouldn't be instantiated alone. You could exchange Abstract Classes for some utility classes with static methods, but than you would loose the advantages of OOP.

peter
  • 14,348
  • 9
  • 62
  • 96