Context
I've recently came across this C# proposal default interface methods I've read both the specification and more importantly the motivation. Possibly I missed something, but the motivation is a bit stinks me.
The only practical difference between interface and a fully abstract class was that a future class can implement (so be [IS A]) multiple interfaces, but can inherit (so be [IS A]) from only one abstract class, (and all the consequences)
What is not clear for me what is the exact difference between abstract classes and interfaces with default methods now, except that we can bring multiple (implementation) inheritance into the picture with default methods, which is not possible with abstract classes. (I do not want open the question/discussion is it good or bad, this is not the topic here)
However the motivation talks about completely different, three points:
Question
My question is what is the real difference (or motivation), or what am I missing?
The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.
Because by default all methods are abstract inside the interface. So this example states that we can not have final methods inside the interfaces. Hence, this example also shows that we can have abstract methods only inside the interface.
The main advantages of interface over abstract class is to overcome the occurrence of diamond problem and achieve multiple inheritance. In java there is no solution provided for diamond problem using classes. For this reason multiple inheritance is block using classes in java.
Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes. Interfaces are a good choice when we think that the API will not change for a while.
They add this feature in Java 8. So you can add the Java
tag and ask to Java developers what they can do with it. It also exists on Haskell
and Scala
apparently.
What come to my mind first is Multi-Inheritance. As a class can implement multiple interfaces, you can for example to solve the diamond problem.
In Java this is how they do:
public interface InterfaceA {
public default void foo() {
System.out.println("A -> foo()");
}
}
public interface InterfaceB {
public default void foo() {
System.out.println("B -> foo()");
}
}
private class Test implements InterfaceA, InterfaceB {
// Compilation error : "class Test inherits unrelated defaults for foo() from types InterfaceA and InterfaceB"
}
So you have to either implement the methods (which override the default implementations) or call one of the super
:
public class Test implements InterfaceA, InterfaceB {
public void foo() {
InterfaceB.super.foo();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With