Skip to content

Instantly share code, notes, and snippets.

@LazaroIbanez
Created September 3, 2017 17:05
Show Gist options
  • Save LazaroIbanez/d2981bf1bddbb1ca2d6013398b8a06a4 to your computer and use it in GitHub Desktop.
Save LazaroIbanez/d2981bf1bddbb1ca2d6013398b8a06a4 to your computer and use it in GitHub Desktop.
Java interface static method is similar to default method except that we can’t override them in the implementation classes.
public class E implements Interf1 {
public String getId() {
return “E: get Id”;
}
public String getOtherId() {
return “E: get Other Id”;
}
public static void main (String… args) {
System.out.println(“Object 1”);
E object1 = new E();
System.out.println(object1.getId());
System.out.println(object1.getOtherId());
System.out.println(“Object 2”);
Interf1 object2 = new E();
System.out.println(object2.getId());
System.out.println(“Calling the static method in Interf1”);
System.out.println(Interf1.getOtherId());
}
}
interface Interf1 {
public default String getId() {
return “Interface 1: get Id”;
}
public static String getOtherId() {
return “Interface 1: get Other Id”;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment