Skip to content

Instantly share code, notes, and snippets.

@shadowfacts
Created July 30, 2015 23:34
Show Gist options
  • Save shadowfacts/1d9828042b2f1125345a to your computer and use it in GitHub Desktop.
Save shadowfacts/1d9828042b2f1125345a to your computer and use it in GitHub Desktop.
Java 8 Interface Defaults example
package net.shadowfacts.example;
/**
* A class that implements Interface1 but only has one custom value.
**/
public class Class1 implements Interface1 {
public String name() {
return "Class1";
}
public int method1() {
return 1;
}
public float method2() {
return 3.1f;
}
public boolean method3() {
return true;
}
}
package net.shadowfacts.example;
/**
* A class that implements Interface2 and is only required to implement 1 method because the other 3 have default implementations in Interface2
**/
public class Class2 implements Interface1 {
public String name() {
return "Class2";
}
}
package net.shadowfacts.example;
/**
* An interface
**/
public interface Interface1 {
String name();
int method1();
float method2();
boolean method3();
}
package net.shadowfacts.example;
/**
* An interface extending Interface1
**/
public interface Interface2 extends Interface1 {
default method1() {
return 1;
}
default method2() {
return 3.1f;
}
default method3() {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment