Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interfaces — What's the point?

Tags:

c#

.net

interface

The reason for interfaces truly eludes me. From what I understand, it is kind of a work around for the non-existent multi-inheritance which doesn't exist in C# (or so I was told).

All I see is, you predefine some members and functions, which then have to be re-defined in the class again. Thus making the interface redundant. It just feels like syntactic… well, junk to me (Please no offense meant. Junk as in useless stuff).

In the example given below taken from a different C# interfaces thread on stack overflow, I would just create a base class called Pizza instead of an interface.

easy example (taken from a different stack overflow contribution)

public interface IPizza {     public void Order(); }  public class PepperoniPizza : IPizza {     public void Order()     {         //Order Pepperoni pizza     } }  public class HawaiiPizza : IPizza {     public void Order()     {         //Order HawaiiPizza     } } 
like image 768
Nebelhom Avatar asked Jul 23 '11 18:07

Nebelhom


People also ask

Do we really need interfaces?

We need interfaces : To achieve total abstraction. To achieve security. Java doesn't allow multiple inheritance but it can be achieved by implementing multiples interfaces.

What is good about interfaces?

Interfaces allow you to do something critical to writing good code: They will enable you to program to an abstraction. A pure abstraction.


2 Answers

No one has really explained in plain terms how interfaces are useful, so I'm going to give it a shot (and steal an idea from Shamim's answer a bit).

Lets take the idea of a pizza ordering service. You can have multiple types of pizzas and a common action for each pizza is preparing the order in the system. Each pizza has to be prepared but each pizza is prepared differently. For example, when a stuffed crust pizza is ordered the system probably has to verify certain ingredients are available at the restaurant and set those aside that aren't needed for deep dish pizzas.

When writing this in code, technically you could just do

public class Pizza {     public void Prepare(PizzaType tp)     {         switch (tp)         {             case PizzaType.StuffedCrust:                 // prepare stuffed crust ingredients in system                 break;                              case PizzaType.DeepDish:                 // prepare deep dish ingredients in system                 break;                              //.... etc.         }     } } 

However, deep dish pizzas (in C# terms) may require different properties to be set in the Prepare() method than stuffed crust, and thus you end up with a lot of optional properties, and the class doesn't scale well (what if you add new pizza types).

The proper way to solve this is to use interface. The interface declares that all Pizzas can be prepared, but each pizza can be prepared differently. So if you have the following interfaces:

public interface IPizza {     void Prepare(); }  public class StuffedCrustPizza : IPizza {     public void Prepare()     {         // Set settings in system for stuffed crust preparations     } }  public class DeepDishPizza : IPizza {     public void Prepare()     {         // Set settings in system for deep dish preparations     } } 

Now your order handling code does not need to know exactly what types of pizzas were ordered in order to handle the ingredients. It just has:

public PreparePizzas(IList<IPizza> pizzas) {     foreach (IPizza pizza in pizzas)         pizza.Prepare(); } 

Even though each type of pizza is prepared differently, this part of the code doesn't have to care what type of pizza we are dealing with, it just knows that it's being called for pizzas and therefore each call to Prepare will automatically prepare each pizza correctly based on its type, even if the collection has multiple types of pizzas.

like image 149
KallDrexx Avatar answered Sep 23 '22 07:09

KallDrexx


The point is that the interface represents a contract. A set of public methods any implementing class has to have. Technically, the interface only governs syntax, i.e. what methods are there, what arguments they get and what they return. Usually they encapsulate semantics as well, although that only by documentation.

You can then have different implementations of an interface and swap them out at will. In your example, since every pizza instance is an IPizza you can use IPizza wherever you handle an instance of an unknown pizza type. Any instance whose type inherits from IPizza is guaranteed to be orderable, as it has an Order() method.

Python is not statically-typed, therefore types are kept and looked up at runtime. So you can try calling an Order() method on any object. The runtime is happy as long as the object has such a method and probably just shrugs and says »Meh.« if it doesn't. Not so in C#. The compiler is responsible for making the correct calls and if it just has some random object the compiler doesn't know yet whether the instance during runtime will have that method. From the compiler's point of view it's invalid since it cannot verify it. (You can do such things with reflection or the dynamic keyword, but that's going a bit far right now, I guess.)

Also note that an interface in the usual sense does not necessarily have to be a C# interface, it could be an abstract class as well or even a normal class (which can come in handy if all subclasses need to share some common code – in most cases, however, interface suffices).

like image 36
Joey Avatar answered Sep 20 '22 07:09

Joey