Created
October 9, 2021 13:55
-
-
Save nowshad-hasan/4e9f538638e25208fe41c08f06d1a3cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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