Can someone help me understand why they would add Default in Java 8 to allow implementation to interfaces instead of just allowing us to have multiple inheritance in abstract classes? What exactly is the difference between just allowing us to extend multiple classes verses allowing us to create Default methods in interfaces?
-
I'm not clear what you mean here. Please educate me on this feature of Java 8 that I may not yet be aware of. – Hovercraft Full Of Eels Mar 11 '15 at 01:05
-
@HovercraftFullOfEels basically you create an interface and you create an ordinary method with implementation but instead you put only Default in front of it, any class that implements the interface will inherit this method as Public with it's implementation if an implementation does not already exist. http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html – CodeCamper Mar 11 '15 at 01:06
-
@HovercraftFullOfEels [Default Methods](http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html) - sounds like a means to avoid having to create `abstract` implementations... – MadProgrammer Mar 11 '15 at 01:06
-
java does that as a workaround to the inability to implement proper features such as C#'s extension methods. It is basically an [ugly hack](http://blog.jooq.org/2014/04/04/java-8-friday-the-dark-side-of-java-8/) because they needed to imitate LINQ (to objects) somehow while keeping java's constant philosophy of looking backwards instead of looking into the future. – Federico Berasategui Mar 11 '15 at 01:08
-
@HighCore What's so great about extension methods? Aren't they just static methods disguised as instance methods? – Paul Boddington Mar 11 '15 at 01:10
-
@MadProgrammer the most pathetic part of the situation is if 2 interfaces have the same default method signature you get a compile time error... *facepalm* that destroys the entire purpose of me being able to implement multiple interfaces. Why would I want to pollute my interfaces and make them possibly unusable with other interfaces? I feel like my question is not answered in the duplicate... why not just allow multiple extends with this ridiculous behavior? – CodeCamper Mar 11 '15 at 01:18
-
@CodeCamper which is why java's "equivalents" of C#'s `Action
` are totally different interfaces with totally diferent method names, including some specialized versions for "primitive" types since java's generics are essentially broken and you can't do `A – Federico Berasategui Mar 11 '15 at 01:20` where T is for example `int`. -
2I really don't see how this question is a duplicate, where does that question answer the difference between simply allowing multiple inheritance which breaks when you have similar method signatures and adding a broken quirky default keyword. – CodeCamper Mar 11 '15 at 01:27
-
@CodeCamper I don't think it's a duplicate either. Allowing `default` methods in `interface`s isn't like allowing multiple inheritance of classes because interfaces still can't have state. I'm a big fan of java, but I wish they hadn't added `default` methods. I agree it's a total mess and people are going to abuse the feature. – Paul Boddington Mar 11 '15 at 01:37
-
Default methods are added for backwards compatibility. They allow adding of new methods to an interface while allowing code that implements the old version to still compile, even though they are missing some of the methods. This was needed to allow some of the api changes in Java 8. – fgb Mar 11 '15 at 01:45
-
@fgb But do the benefits the api changes have given us outweigh the mess created by the insistence on maintaining backwards compatibility at every change? I think it's debatable. – Paul Boddington Mar 11 '15 at 01:57
-
"Doesn't Default in Java 8 destroy the whole idea of not allowing us to do multiple inheritance?" As you can only inherit public methods, the class that implements interfaces with default methods would be more a trait than a class that inherits attributes and behaviour from multiple superclasses. So, technically, no; default methods don't destroy the idea of not allowing multiple inheritance. – fps Mar 11 '15 at 02:09
2 Answers
No, default doesn't destroy the idea of not allowing multiple inheritence, at least not more than interfaces already do. The point is still that interfaces only specify an interface (method signatures and return values), whereas classes also specify the state of an object. This idea in not broken by default methods, that is, default methods only have access to the methods defined in the interface, not to any member variables (because interfaces cannot define member variables).
So with respect to state, each class still has a single superclass. But with respect to interface, it can have arbitrarily many supertypes.

- 12,677
- 8
- 34
- 50
-
1I assume you meant _default doesn't destroy the idea of **multiple** inheritance_, If so, better correct it. – CoronA Mar 11 '15 at 08:11
-
Can someone help me understand why they would add Default in Java 8 to allow implementation to interfaces instead of just allowing us to have multiple inheritance in abstract classes?
One important reason is super class state. For example;
class OpticalDrive {
}
class DVDDrive extends OpticalDrive {
protected int speed = 5;
}
class CDDrive extends OpticalDrive {
protected int speed = 10;
}
class ComboDrive extends DVDDrive, CDDrive {
ComboDrive() {
System.out.println(super.speed);
super.speed = 15;
}
}
In above hypothetical example speed
state variable is multiple inherited and reading or modifying it creates ambiguity as to which one must be modified.
What exactly is the difference between just allowing us to extend multiple classes verses allowing us to create Default methods in interfaces?
Since interfaces can't have mutable state variables it is better to have default methods in interfaces rather than enabling multiple inheritance of abstract classes. Following example shows Java 8 default method in interface in action.
public interface DvdReader {
default public void read() {
System.out.println("DvdReader.read()");
}
}
public interface CdReader {
default public void read() {
System.out.println("CdReader.read()");
}
}
public class ComboReader implements CdReader, DvdReader {
public void read() {
CdReader.super.read();
DvdReader.super.read();
}
public static void main(String[] args) {
new ComboReader().read();
}
}
In the above scenario if you ignore overriding read
method in ComboReader
, the compiler will complain with the following error.
ComboReader.java:1: error: class ComboReader inherits unrelated defaults for read() from types CdReader and DvdReader
public class ComboReader implements CdReader, DvdReader {
^
1 error
Furthermore enabling multiple abstract class inheritance means the syntax of Java code has to change significantly causing java's ability to backward compatibility.

- 21,379
- 3
- 54
- 71
-
By not allowing the same default method signatures in 2 different interfaces, doesn't that break backward compatibility? What if I have interfaces that have the same default method name as other interfaces, it is bound to happen the larger the interface... Is there any advice on how to avoid this approaching conflict in the future? – CodeCamper Mar 11 '15 at 11:27