قالب وردپرس درنا توس
Home / Tips and Tricks / What does an interface do in object-oriented programming? – CloudSavvy IT

What does an interface do in object-oriented programming? – CloudSavvy IT



One of the main components of object-oriented languages ​​such as Java and C # is the ability to write classes using interfaces, which standardize method definitions and allow for improved polymorphism. We discuss what they are and how we can use them.

Formalized Polymorphism

Interfaces are basically classes without code. They can define properties and methods (but not directly fields) without actually storing data or writing an implementation for functions.

For example, the IEnumerable interface of .NET is very common. All it takes is a function that returns an IEnumerator, which can be used to iterate over a collection.

  1. public interface IEnumerable
  2. {
  3.     IEnumerator GetEnumerator();
  4. }

Any collection in .NET implements this interface. In fact, most collections will implement many interfaces, all of which standardize the methods used with them.

The advantage of doing it this way is that all the methods are standardized in the interface, so anytime you want something that accepts many kinds of classes, you can do it by going to the interface methods.

For example, foreach use loops under the hood .GetEnumerator()so that they support any type of collection that is being implemented IEnumerable, such as lists, dictionaries, and HashSets.

  1. void Main()
  2. {
  3.     foreach(var item in  IEnumerable) 
  4.     {
  5.        // do something
  6.     }
  7. }

You can think of Interfaces as a substitute for base classes. Base classes are still very useful, but it can be inconvenient to require each class to inherit and override methods. In addition, you can only have one base class, but you can implement any number of interfaces.

Interfaces can also be used instead of type parameters. For example, you have a custom collection, CustomList. This works for any type, but if you want to call a specific method for each element, you can’t, as the compiler has no idea if the type used supports that method. The only options you will get are basic methods objects.

However, if you were to remove the generic type parameter and use an interface instead, you can call a method on the items. The collection still supports any type, although now any item you want to put into it will have to implement the interface.

In general, you should use interfaces if you reuse the same methods for many classes that don’t inherit from each other, and you would like to write polymorphic code that doesn’t care about the specific underlying class.

Using interfaces

Interfaces are quite easy to use. They use the same inheritance syntax as base classes, a colon after the class definition. Note that if you want to use an interface with a base class, the base class must come first, followed by a comma and then any interfaces it implements.

After adding the interface definition, you will likely get an error. Visual Studio will tell you that you are not actually implementing the interface properties and methods. This can actually be really useful because if you make changes to the interface, you have to update all the classes that implement it.

Visual Studio is pretty classy, ​​and if you click on “show possible fixes”, you get an option for VS to automatically add the default interface interface to your class. You will of course have to replace the NotImplementedExceptions.

You can also implement interfaces expressly. This has the same effect, but with a different syntax that clarifies which interface the method or property originated from. This looks strange at first, but can be useful if you implement a lot of interfaces.

Writing your own interfaces

Interface definitions are much the same as class definitions, although each method has no body, and you will of course need to use “interface” instead of “class”. Like classes, interfaces can also use generic type parameters, which can be useful for custom collections.

You can use the { get; set; } syntax to specify properties implicitly, which also works when implementing it in the actual class:

You can also inherit interfaces. This for example ICustomList interface could be much more useful if it just took all the other definitions provided List used, but adds a few custom ones. If you are going to use the interface now, you must implement all interface members of every interface it inherits.


Source link