“Schnittstelle in Java” Code-Antworten

Schnittstelle in Java

An interface is a special kind of class that cannot be instantiated.
 The main idea of an interface is declaring functionality. Interfaces help to 
 abstract from specific classes and emphasize the functionality. 
It makes software design more reusable and clean
Opposite to a class, an interface can extend several interfaces. 
A class can implement multiple interfaces.
coder

Schnittstelle in Java

An interface can contain:

public constants;
abstract methods without an implementation (the keyword abstract is not required here);
default methods with implementation (the keyword default is required);
static methods with implementation (the keyword static is required);
private methods with implementation.
Java 9 onwards, you can include private methods in interfaces. Before Java 9 
it was not possible.
An interface can't contain fields (only constants), constructors,
 or non-public abstract methods.
The keyword abstract before a method means that the method does not have a
body, it just declares a signature.
coder

Schnittstelle in Java

In many cases, it is more important to know what an object can do,instead of 
how it does what it does. This is a reason why interfaces are commonly used for
declaring a type of variable.
 interface : DrawingTool : a tool can draw
 interface DrawingTool {
    void draw(Curve curve);
}
DrawingTool pencil = new Pencil();
DrawingTool brush = new Brush();
Both Pencil and brush class should implement draw method
coder

Schnittstelle in Java

the main idea of an interface is declaring functionality.
Suppose you program a game that has several types of characters.
These characters are able to move within a map. That is represented by Movable
interface
coder

Schnittstelle in Java

interface Interface {
        
    int INT_CONSTANT = 0; // it's a constant, the same as public static final int INT_FIELD = 0
        
    void instanceMethod1();
        
    void instanceMethod2();
        
    static void staticMethod() {
        System.out.println("Interface: static method");
    }
        
    default void defaultMethod() {
        System.out.println("Interface: default method. It can be overridden");
    }

    private void privateMethod() {
        System.out.println("Interface: private methods in interfaces are acceptable but should have a body");
    }
}
Static, default, and private methods should have an implementation in the interface!
coder

Schnittstelle in Java

/* File name : Animal.java */
interface Animal {
   public void eat();
   public void travel();
}
Mattalui

Ähnliche Antworten wie “Schnittstelle in Java”

Fragen ähnlich wie “Schnittstelle in Java”

Weitere verwandte Antworten zu “Schnittstelle in Java” auf Java

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen