https://abs.twimg.com/emoji/v2/... draggable="false" alt="❓" title="Rotes Fragezeichen-Symbol" aria-label="Emoji: Rotes Fragezeichen-Symbol"> Have you ever wondered what interfaces are for in object-oriented languages?

They are more than a just a class with abstract methods.

They play a key role in many design patterns and principles.

A thread https://abs.twimg.com/emoji/v2/... draggable="false" alt="🧵" title="Thread" aria-label="Emoji: Thread"> about 6 key aspects of interfaces.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="1⃣" title="Tastenkappe Ziffer 1" aria-label="Emoji: Tastenkappe Ziffer 1"> Interfaces establish knowledge boundaries.

When you call an interface method, you can often ignore what is under the hood.

Example: class & #39;C& #39; need not know the exact database that stores the data.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="🔑" title="Schlüssel" aria-label="Emoji: Schlüssel"> Interfaces reduce complexity because they allow us to separate concerns.
https://abs.twimg.com/emoji/v2/... draggable= Interfaces establish knowledge boundaries.When you call an interface method, you can often ignore what is under the hood.Example: class & #39;C& #39; need not know the exact database that stores the data.https://abs.twimg.com/emoji/v2/... draggable="false" alt="🔑" title="Schlüssel" aria-label="Emoji: Schlüssel"> Interfaces reduce complexity because they allow us to separate concerns." title="https://abs.twimg.com/emoji/v2/... draggable="false" alt="1⃣" title="Tastenkappe Ziffer 1" aria-label="Emoji: Tastenkappe Ziffer 1"> Interfaces establish knowledge boundaries.When you call an interface method, you can often ignore what is under the hood.Example: class & #39;C& #39; need not know the exact database that stores the data.https://abs.twimg.com/emoji/v2/... draggable="false" alt="🔑" title="Schlüssel" aria-label="Emoji: Schlüssel"> Interfaces reduce complexity because they allow us to separate concerns." class="img-responsive" style="max-width:100%;"/>
https://abs.twimg.com/emoji/v2/... draggable="false" alt="2⃣" title="Tastenkappe Ziffer 2" aria-label="Emoji: Tastenkappe Ziffer 2"> Interfaces establish architectural boundaries.

The separation in the previous example is also "physical".

You can change the & #39;Database& #39; module without affecting the & #39;C& #39; module.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="🔑" title="Schlüssel" aria-label="Emoji: Schlüssel"> Interfaces allow us to design systems that are loosely coupled and therefore more testable.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="3⃣" title="Tastenkappe Ziffer 3" aria-label="Emoji: Tastenkappe Ziffer 3"> Interfaces define a communication protocol.

The buttons of your calculator are its interface.

When you push & #39;+& #39;, you send the & #39;sum& #39; message.

Same with objects.

When A calls a method on B, A sends a message to B.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="🔑" title="Schlüssel" aria-label="Emoji: Schlüssel"> Interfaces define the messages objects understand.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="4⃣" title="Tastenkappe Ziffer 4" aria-label="Emoji: Tastenkappe Ziffer 4"> Interfaces establish a contract.

They set:

https://abs.twimg.com/emoji/v2/... draggable="false" alt="🔹" title="Kleine blaue Raute" aria-label="Emoji: Kleine blaue Raute">Expectations

If you call "setSide" on a square, you expect all 4 sides to change.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="🔹" title="Kleine blaue Raute" aria-label="Emoji: Kleine blaue Raute">Obligations

If you implement "setSide", you must change 4 sides or you& #39;ll break the contract.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="✏️" title="Bleistift" aria-label="Emoji: Bleistift"> Have a look at: Liskov Substitution Principle
https://abs.twimg.com/emoji/v2/... draggable="false" alt="5⃣" title="Tastenkappe Ziffer 5" aria-label="Emoji: Tastenkappe Ziffer 5"> Interfaces make polymorphism possible.

You can write a function that uses "Shape" and it will work for "Square" and "Circle".

If you ever need "Triangle", you don& #39;t have to modify the function.

This is how you write abstract code.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="✏️" title="Bleistift" aria-label="Emoji: Bleistift"> Have a look at: Open-Closed Principle.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="6⃣" title="Tastenkappe Ziffer 6" aria-label="Emoji: Tastenkappe Ziffer 6"> Interfaces allow you to manage dependencies.

Control flow can go from class & #39;C& #39; to class & #39;Database Access& #39; without the former depending on the latter.

Control flow dependencies need not mirror source code dependencies.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="✏️" title="Bleistift" aria-label="Emoji: Bleistift"> Have a look at: Dependency Inversion Principle.
In summary:

https://abs.twimg.com/emoji/v2/... draggable="false" alt="✨" title="Funken" aria-label="Emoji: Funken"> Interfaces are a key part of object-oriented languages https://abs.twimg.com/emoji/v2/... draggable="false" alt="✨" title="Funken" aria-label="Emoji: Funken">

They allow you to create well-designed systems that:

https://abs.twimg.com/emoji/v2/... draggable="false" alt="✅" title="Fettes weißes Häkchen" aria-label="Emoji: Fettes weißes Häkchen"> Keep complexity under control.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="✅" title="Fettes weißes Häkchen" aria-label="Emoji: Fettes weißes Häkchen"> Separate concerns.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="✅" title="Fettes weißes Häkchen" aria-label="Emoji: Fettes weißes Häkchen"> Are modular, loosely coupled, and testable.
https://abs.twimg.com/emoji/v2/... draggable="false" alt="✅" title="Fettes weißes Häkchen" aria-label="Emoji: Fettes weißes Häkchen"> Are easy to change.

This is power https://abs.twimg.com/emoji/v2/... draggable="false" alt="⚡️" title="Hochspannungszeichen" aria-label="Emoji: Hochspannungszeichen">
To end the thread, a disclaimer:

I& #39;ve written this thread with statically typed languages in mind.

In dynamically typed languages, interfaces are usually an implicit concept, so some of the ideas apply and others don& #39;t.

Maybe, I can prepare a thread about this one day https://abs.twimg.com/emoji/v2/... draggable="false" alt="🤔" title="Denkendes Gesicht" aria-label="Emoji: Denkendes Gesicht">
You can follow @macerub.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: