Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Understanding Interfaces can be challenging for many beginning programmers. Hereâs a super simple guide to understanding them.
The classic explanation is that âan Interface is a contractâ. This is what the people interviewing you for a job expect to hear. But what does âan interface = a contractâ mean?
Well, first off, we know that Interfaces contain a special format of code that does not perform any tasks itself, that is, an Interface may have method signatures (the beginning part of a method), but all the code functionality is missing! The actual method code is contained in the class which only implements (the Interface version of inheritance) the interface.
// The Interface// Notice that the interface only has a method signature. The method has no body.
public interface IExampleInterface{ // return type, method name, required parameter types and names void Method1(string parameter1, string parameter2);}
Interfaces can also contain properties and events, in addition to method signatures.
// The Class// Implement the inteface by having the class "inherit" it.
public class MyExampleClass : IExampleInterface{ void Method1(string parameter1, string parameter2) { // do something }}
// Notice that the method signature here is exactly the same as the one in the interface.
When a class implements a specific Interface, the class promises to follow the specific rules and regulation defined in that special code contained in the Interface. In our example, our Interface contains several method signatures. When our class implements the Interface, it promises to at the very minimum contain methods that match those method signatures exactly. The Interface doesnât care about the code inside the method, it just requires that, just like I have { X } number of method signatures (or properties), the implementing class has the same exact number (at the very minimum) and that they match exactly.
A good example of how this contract works would be to look at how a franchised business works. Many people patronize specific companies due to âbrand recognitionâ. Think of a large, chain, business near you. Letâs use Starbucks, for example. Starbucks is well known and has established itself as a trustworthy coffee shop where you are guaranteed to receive a certain level of service and product. Now, if you wanted to open a coffee shop as a small business, would you choose to open an independent coffee shop or would you pay a franchising fee to Starbucks to use their logo and marketing, as well as having to follow their corporate rules and regulations? Well, while an indie coffee shop provides more flexibility, customers may be less willing to patronize your indie shop since the service and product you provide is of an unknown quality and is also subject to change at any time without warning. A franchised Starbucks location on the other hand, is a known quantity due to your âcontractâ with the Starbucks corporation. Everyone knows what theyâre getting, there are clearly laid out policies for everything including returns, refunds, and other customer satisfaction issues. Starbucks did a lot of the early, difficult, work of creating a brand that customers recognize and trust, you just must follow their corporate rules in order to benefit from that hard work.
In our case, the small business owner would be comparable to the class and the Starbucks corporation would be the Interface. The âcontractâ (that your interviewer is expecting you to mention) is the contract that you, as a business owner, sign with the corporation (Starbucks/the Interface) in order to benefit from using their work. Our Interfaces have a set of rules that we must follow, and this, in both business and software development, keeps things nice, tidy, organized, and predictable.
Finally! The Real Simple Guide To Understanding Interfaces was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.