Skip to content

Instantly share code, notes, and snippets.

@vnnvanhuong
Created July 26, 2017 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vnnvanhuong/a71705a63c0c9a3fa3cfbaddb18c7029 to your computer and use it in GitHub Desktop.
Save vnnvanhuong/a71705a63c0c9a3fa3cfbaddb18c7029 to your computer and use it in GitHub Desktop.
package vn.nvanhuong.java_lab;
//Inheritance from defined default methods
interface Foo{
default void method() {
System.out.println("Foo");
};
}
interface Bar{
default void method() {
System.out.println("Bar");
};
}
class MyClass implements Foo, Bar{
@Override
public void method() {
Foo.super.method(); //compiler forces to explicitly define this if inheritance
}
}
//Inheritance from defined static methods
class StaticFoo{
static void staticMethod() {
System.out.println("StaticFoo");
}
}
class StaticBar extends StaticFoo{
static void staticMethod() {
System.out.println("StaticBar");
}
}
class MyStaticClass extends StaticBar{
static void myStaticMethod() {
staticMethod(); //default -> uses from direct parent
StaticFoo.staticMethod(); //users must explicitly defines if inheritance
}
}
//Result
public class MultiInheritance {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.method();
//Foo
MyStaticClass.myStaticMethod();
//StaticBar
//StaticFoo
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment