What are interfaces main purposes and what features do they offer to give us a solid reason to use them?

A little history here: the original impetus for interfaces, when they were introduced in Java (circa 1992, when it was still code-named Oak) was as an end-run around multiple inheritance. Some languages, such as C++ and Smalltalk-80, allowed a class to inherit from more than one parent class; this was a very powerful but very tricky feature, one which caused many problems for those who tried to use it. It was also a nightmare to implement efficiently, as it turned inheritances from a simple tree to a potentially much more complex directed acyclic graph, which made the process of resolving method dispatch a real bear.

By the time Java came along, there was a definite dislike of multiple inheritance in much of the industry, but single inheritance presented the problem that it made for an artificially rigid hierarchy: you often were forced to repeat both the class signature and implementation details, and many logically reasonable connections couldn't be made. Java's solution was the interface: a mechanism for multiple inheritance of the class signatures, without the problems associated with multiple inheritance of the actual methods.

This turned out to have it's own problems, but it also had unforeseen advantages, so when C# was designed, they copied the Java mechanism almost exactly.

Edited 2 Years Ago by Schol-R-LEA

Hi, From a personal perspective, I've only used interfaces in Java to implement API's. Where a class implements an interface it contracts / guarantees to provide variables and named methods as demanded by the interface.

So for example if an interface defines a 'shape' api it might specify as a minimum the existance instance variables of height and width and a method to calculate area for any class implementing the API to the class 'shape'.

Classes are ofcourse free to implement other variables and methods to extend utility but guarantee to meet the minimum demanded by the interface.

I think its helpful to make a stride back and contemplate the saying "interface" in a general sense. There are interfaces all over in programming. There are interfaces between layers, between levels, between applications, in the middle of articles, and in the middle of guests and their callees. Pretty much everything without exception in programming, regardless of how minor, has an interface.

Interfaces represents the functionality you want to have in a class. They are used in IoC containers for injecting the concrete class instances.

Interfaces are the contracts you need to follow while you are creating your class. Interfaces are different from the abstract classes, as interfaces simply tell you what methods you need to use in your class. You could implement more than one interfaces in your class(you are signing different contracts) but you can t extend your class to more than one class. Many different design patterns were created using interfaces for Eg Adapter pattern uses Interfaces to let you add a new class into your existing framework using interfaces or state pattern lets your object have different states using methods you define in your interface(s).

You could watch this tutorial to understand polimorphism better
https://www.youtube.com/watch?v=TZi86NDFwNU

This article has been dead for over six months. Start a new discussion instead.