Created
October 9, 2021 13:52
-
-
Save nowshad-hasan/22904846eb2d00ccef86c92f4ae720ed to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Which classes are permitted to extend this Liquid class, must be sealed/non-sealed/final. | |
public sealed class Liquid permits Water, Honey, Milk {} | |
// Now, Water can be extended by any class | |
public non-sealed class Water extends Liquid {} | |
// Honey is final. So, it can't be extended by anyone. | |
public final class Honey extends Liquid {} | |
// Milk is sealed. So, it starts another sealed hierarchy. | |
// Which class extends Milk, must be sealed/non-sealed/final. | |
public sealed class Milk extends Liquid permits Yogurt {} | |
// Yogurt completes sealed hierarchy, making itself final | |
public final class Yogurt extends Milk {} | |
// Tea can't extend Liquid class, as it was not permitted. But it can extend Water as this is non-sealed. | |
public class Tea extends Water {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment