2

I am clear before Java 8 interfaces are fully abstract but in Java 8, I saw

public interface A{
    public static void staticMethod() {
        //implentation ...
    }

    public default void defaultMethod() {
       //implementation ...
    }
}

public interface B{
        public static void staticMethod() {
            //implentation ...
        }

        public default void defaultMethod() {
            //implementation ...
        }
}

public class C implements A,B{
        public static void staticMethod() {
            //implentation ...
        }

        public default void defaultMethod() {
            //implementation ...
        }

}

I learned that Java did not allow multiple inheritances because of the diamond problem, now the diamond problem comes in Java 8, we need to solve this by implementing both methods in class C.

I need to know after java 8 can we consider java support multiple inheritances or Not?

still in java classes cannot have multiple inheritances so if some one ask, is Java 8 support multiple inheritances, looking only above example can I say? yes or partially support?

shehan
  • 31
  • 4
  • 1
    It's as multiple-inheritance as it was before. That where an implementation comes from has slightly changed is largely irrelevant, just a bit more convenient. – Dave Newton Nov 18 '18 at 18:09
  • It's more complex than that... but it's a bad question for the SO format. You will find much better explanations by browsing the discussions in the Internet at large. – fdreger Nov 18 '18 at 18:11
  • @ Dave Newton is it mean java not allow multiple inheritance – shehan Nov 18 '18 at 18:11
  • @ fdreger, i am sorry if this is bad, but i seach on internet and read about this but i cannot find satisfing solution – shehan Nov 18 '18 at 18:14

1 Answers1

5

The diamond problem to which you're referring involves the class attempting to inherit both methods (defaultMethod in your case). Because C is overriding defaultMethod, the diamond problem does not apply here.

But let's say you remove it. This creates the diamond problem that you would see in languages like C++. Java has decided that a class cannot inherit two default implementations with the same signature, and will fail to compile if such an attempt is made. Such classes would be required to provide an explicit override.

Joe C
  • 15,324
  • 8
  • 38
  • 50
  • yeh, I want to give an example of how the diamond problem arises and how java solves it. sorry if I gave the wrong impression by giving an example – shehan Nov 18 '18 at 18:30
  • The second paragraph in my answer attempts to answer this. If you require any clarification, please feel free to ask. – Joe C Nov 18 '18 at 19:35