

We would remain happy as our existing class requireS no extra work and will still compile.
Java interface with default methods code#
By using this example I believe it is more important for API / library developers to consider using default methods than it is for those that are writing code within their own codebase where they are in control of everything and can change any classes that have been broken by adding a new interface method.īelow are some code snippets to help make the above point clearer (if it wasn’t already). So if we had a class that used an interface that was changed, for example code within a 3rd party library, we wouldn’t need to worry about any new methods being added to the interface as our existing code will still work and the new method can be ignored until a later date. So why use default methods in the first place? After doing some googling, it seems that they were added to Java 8 as a way of adding methods in preparation for Lambda expressions without breaking code that implemented existing interfaces. This keeps it in line with providing an implementation to a default method from within a class, if it is included in the child / implementation then it will use that instead of the original defined on the parent interface.


Below is a code snippet of what this would look like.Īs you can see it has called defaultMethod from within the child interface MyOtherInterface. This makes sense as super normally refers to the extended class and as multiple interfaces are allowed restricting it to always being called like this keeps it consistent and without any ambiguity. But there is one catch, you need to put the name of the interface before calling super this is necessary even if only one interface is added. A class can override a default interface method and call the original method by using super, keeping it nicely in line with calling a super method from an extended class. To get around this situation we need need to provide an implementation that will override the versions that the interfaces are giving the class which will be used instead and thus removing the ambiguity which resulted in the error. Java: class M圜lassWithTwoInterfaces inherits unrelated defaults for defaultMethod()įrom types MyInterface and MyOtherInterfaceĪs you can probably figure out, it cannot determine which default method it should actually use so it blows up.
Java interface with default methods how to#
To add a default method just add the keyword default to the method definition and I’m not even going to tell you how to add a private method as I don’t wish to insult you!īeing it’s so simple lets just jump straight into some code examples. In terms of adding default and private methods to an interface, its really simple. So now that both default and private methods can exist within an interface we can write methods like we are used to, although if you haven’t used default methods yet then you will first need to get past the fact that there is now actual code living in an interface. This is something that was a bit off putting to me as if you had a default method that became a bit long there was no way to tidy it up. Private methods were missing when default methods were added, preventing code being split out into smaller methods within an interface. Default methods were added in Java 8 allowing methods to be added to an interface that comes with a default implementation that could be used, overridden or ignored without causing issues to existing classes that have implemented an interface. They look something like this: public interface HasXY Īt this time I have the option to refactor this code into an interface and a class.In this tutorial we will look at default and private methods within interfaces. In a code base I am "starting from" there are a handful of interfaces with many default methods.
