Why interface methods do not have a body

To achieve multiple inheritance, we must use interfaces, but why do interface methods not have bodies and why should they be overridden in a derived class?

I really need a clear answer, not related to too much computer jargon, I don't seem to understand this, I mentioned various links

+5
source share
8 answers

Because Java, unlike languages ​​like C ++ or Eiffel, has only multiple type inheritance (e.g. interfaces, as well as one class), and not multiple state and behavior inheritance. The latter ones add tremendous complexity (especially state).

Java ( #, ) , ++ . , , .

, ( ) Java 8 ( , ) , , , , .

+13

WHAT-, , ( ). , .

+2

(public + static + final) ( ). , .

: " ", , , ( ).

Java, , 1 , . Java .

+2

Java , , . , : (es).

+1

:

.

:
  Java , . ,

public interface Comparable      
{   boolean less(Object m);
    boolean greater(Object m);
    boolean lessEqual(Object m);
    boolean greaterEqual(Object m);
}

.
, . Java , java.lang.Object ( Java); java.

public abstract. , , . , , , .

, . , ( ) , . . , , . ( .) ( - ). , C++. , , .

Polynomial, Comparable, , .

public class Polynomial implements Comparable
{   . . .
    boolean less(Object m){ . . . }
    boolean greater(Object m){ . . . }
    boolean lessEqual(Object m){ . . . }
    boolean greaterEqual(Object m){ . . . }

    Polynomial multiply(Polynomial P){ . . . }
    . . .
}

. , , . , , , .

. . , , .

:
Interface

Wiki

+1

public interface Flyable
{
public void fly();
}

.

, , , ,   , . , .

, Flyable , , FlyableObjects .

:

class Rocket implements  Flyable 
{
public void fly()
{
// do the stuffs.
}
}

interface

, , .

0

, .

,

interface A {
    void print(){ System.out.print("A") }
}

interface B {
    void print(){ System.out.print("B") }
}

interface C extends A, B {
    // now since A and B have bodies interfaces would have had choice to not to override the default behavior
}

public class C_Implementer implements C{
    public static void main(String args[]){
        C c = new C_Implementer();
        c.print(); // gotcha!!!!! what should it print, A or B????
    }
}
0

: " Java ?"

Java, , , ( ).

( Java, ), .

public interface FaceOne {
    public void method() {
        System.out.println("FaceOne Version");
    }
}

public interface FaceTwo {
    public void method() {
        System.out.println("FaceTwo Version");
    }
}

, .

public class Inheriter implements FaceOne, FaceTwo {

}

Inheriter.method(), , , : " " " "?

, , super, .

Java .


, , :

, . .

super. Inheriter :

  • super.method(), .

  • Use FaceOne.super.method()to make the inherited default implementation result "FaceOne Version".

  • Use FaceTwo.super.method()to make the inherited default implementation result "FaceTwo Version".

  • Use a combination of the above:

One implementation may be:

@Override
public void method() {
    FaceOne.super.method();
    FaceTwo.super.method();
    System.out.println("Inheriter Version");
}

Withdrawal:

FaceOne Version

FaceTwo Version

Inheriter version


Edit: according to this question , this seems to be exactly how the default implementations are structured in Java 8.

0
source

All Articles