Skip to content

Instantly share code, notes, and snippets.

@nowshad-hasan
Created October 9, 2021 13:55
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 nowshad-hasan/4e9f538638e25208fe41c08f06d1a3cd to your computer and use it in GitHub Desktop.
Save nowshad-hasan/4e9f538638e25208fe41c08f06d1a3cd to your computer and use it in GitHub Desktop.
/*
Interface can be sealed or non-sealed, and it can permit other interface or class.
Permitted interface must be sealed/non-sealed and class must be sealed/non-sealed/final.
Sealed interface and permitted interface/class must be in same module. For unnamed module, they must be in same package.
*/
public sealed interface Move permits Fly, Run {
void move();
}
// Fly is non-sealed. So, it can be implemented by any class or extended by any interface.
non-sealed interface Fly extends Move {
}
// starts another sealed hierarchy
sealed interface Run extends Move permits Marathon {
}
// As Fly is made non-sealed, Bird easily implements it without extra keyword.
class Bird implements Fly {
@Override
public void move() {
}
}
// Marathon needs to be final/sealed/non-sealed, as Run is sealed.
final class Marathon implements Run {
@Override
public void move() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment