Skip to content

Instantly share code, notes, and snippets.

@nipafx
Last active January 18, 2016 07:34
Show Gist options
  • Save nipafx/8f04cc7725fb5bcf4949 to your computer and use it in GitHub Desktop.
Save nipafx/8f04cc7725fb5bcf4949 to your computer and use it in GitHub Desktop.
It is not possible for a class to call a default implementation on an interface not mentioned in the 'implements' clause.
public class CallingDifferentSuperMethods {
private interface DefaultImplementing {
default boolean implemented() {
return true;
}
default boolean overridden() {
return true;
}
default boolean reabstracted() {
return true;
}
}
private interface Extending extends DefaultImplementing {
default boolean overridden() {
return false;
}
boolean reabstracted();
}
private static class Implementing implements Extending {
@Override
public boolean implemented() {
// compile error:
// "'DefaultImplementing.DefaultImplementing' in not an enclosing class"
return DefaultImplementing.super.implemented();
// compiles
return Extending.super.implemented();
}
@Override
public boolean overridden() {
// same compile error as in 'implemented()'
return DefaultImplementing.super.overridden();
// compiles
return Extending.super.overridden();
}
@Override
public boolean reabstracted() {
// same compile error as in 'implemented()'
return DefaultImplementing.super.reabstracted();
// compile error:
// "Abstract method 'reabstracted()' can not be accessed directly."
return Extending.super.reabstracted();
}
}
private static class Implementing2 implements DefaultImplementing, Extending {
@Override
public boolean implemented() {
// compile error:
// "Bad type qualifier in default super call: redundant interface
// DefaultImplementing.DefaultImplementing is extended by
// DefaultImplementing.Extending
return DefaultImplementing.super.implemented();
// compiles
return Extending.super.implemented();
}
@Override
public boolean overridden() {
// same compile error as in 'implemented()'
return DefaultImplementing.super.overridden();
// compiles
return Extending.super.overridden();
}
@Override
public boolean reabstracted() {
// same compile error as in 'implemented()'
return DefaultImplementing.super.reabstracted();
// compile error:
// "Abstract method 'reabstracted()' can not be accessed directly."
return Extending.super.reabstracted();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment