What is the difference between the метод по умолчанию declared in the interface with the default modifier and the usual method declared in a regular class?

( update ) I read

The default method is a method that is declared in the interface with the default modifier; his body is always represented by a block. It provides a default implementation for any class that implements an interface without method overlapping. The default methods are different from the specific methods that are declared in the classes.

    2 answers 2

    The default method cannot access the state of the object (the fields of the object), since there is no object, but it can call other methods and access static data (constants).

    The default method avoids the need to change all the classes that implement this interface.

    In a class that implements the interface with the default methods, you can override them.

     interface I1 { // это public static final int i = 0; // но в описании интерфейса public static final можно опустить int i = 0; default void m1() { System.out.println("I1 m1 i = " + i); m2(); } void m2(); } 

    Without default this class would not compile:

     public class C1 implements I1 { @Override public void m2() { System.out.println("C1 m2"); } public static void main(String[] args) { new C1().m1(); new C1().m2(); } } 

    The output will be as follows:

     I1 m1 i = 0 C1 m2 C1 m2 
    • Many beginners forget that the interface does not have access to the state. that's why I mentioned it. in this case, it is a figure of speech. - Mikhail Vaysman

    One of the differences is that all methods (including and default ) in the interface are implicitly declared as public and this access modifier cannot be changed, and you can set various access modifiers in the usual method in the class.

    UPD:

    The default method is a method that is declared in the interface with the default modifier; his body is always represented by a block. It provides a default implementation for any class that implements an interface without method overlapping. The default methods are different from the specific methods that are declared in the classes.

    You have somewhat misunderstood this sentence. Here it was meant that if a class implements an interface that contains a default method, then when overridden in the class of this method, the method from the class will override the default implementation.

    Try to focus on the selected text, then you will understand what I mean:

    It provides a default implementation for any class that implements an interface without method overlapping . The default methods are different from the specific methods that are declared in the classes.

    In addition, if there were any fundamental differences, they would have been indicated here, since in this paragraph from the chapter on interfaces it is told directly about default methods.

    • `@post_zeew: I am very carefully thinking about your clarification, thank you - TimurVI
    • you are right, apparently I hurried to invent differences. Do I somehow reformulate the question? - TimurVI
    • @TimurVI, I think it is better to leave as is. Since at least one difference I described. Although this distinction applies to all interface methods, not just default . - post_zeew
    • And the second difference was cited by @Mikhail Vaysman (in the first sentence of the answer), although it also applies to all interface methods. - post_zeew