What is the use of default interface methods?

Asked

Viewed 100 times

6

I was seeing some features proposed for C# 8 and came across the default interface methods.

Add support for virtual Extension methods - methods in interfaces with Concrete implementations.

From what I understand it consists in allowing implementations within interfaces, but what is the use of it? This does not conflict with the use of abstract classes?

1 answer

6


In fact there is no conflict, abstract classes are classes, so a class can only be inherited from a single class. Interfaces can still be inherited as many as needed, even with standard implementation methods.

This in itself already greatly enhances the usefulness of it since at bottom it is almost never necessary multiple subclasses, but multiple subtypes is important, and types can benefit greatly from having behaviors.

Note that interfaces will still not be able to be, so at this point also differs from abstract classes. It is not yet certain about everything the new functionality will provide, may have other limitations, for example not having constructs, which makes sense since these are usually used to initialize state, which the interface does not have. And in fact after release there may still be a few extra things in future versions.

The reuse of code increases a lot. Today the solution goes through:

  • a) implement the interface in the class with a new code written there
  • b) implement the interface with simple delegation method for a utility method
  • c) abandon the polymorphism of the method (extension method)

Version 8 of C# allows none of this to be required and the implementation is already available for all classes you want to declare to be of that interface subtype (but there are access limitations).

Some considerations can be made:

  • no interface method needs to be implemented in it, it is an option (more or less if you need this method in context other than the interface)
  • the class can always override the implementation of the interface method, it will be mandatory virtual (there are ideas of allowing non-virtual methods under specific conditions, but this is not right)
  • the most interesting for the question is that it will be possible to add a method to an existing interface and not to break all the classes that implement it, as long as this new method has a standard implementation, so that everything can be run.

The initial idea was to do something even better, but to keep compatibility with Java 8 and power interoperate well on Android preferred to do almost equal to Java.

In a way this new functionality improves the capacity of extension methods, which still has its usefulness, even may have other extension possibilities still in C# 8.

One cool thing is that you can make a class conform to an interface without the class even knowing it, everything from the interface will be implemented outside the class. This has advantages and disadvantages, but will greatly help avoid certain design standards that are adopted nowadays.

  • 1

    Just one important caveat: "Interfaces can still be inherited as many as necessary". Implemented, ungrounded.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.