Skip to content

Instantly share code, notes, and snippets.

@nowshad-hasan
Created October 9, 2021 13:52
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/22904846eb2d00ccef86c92f4ae720ed to your computer and use it in GitHub Desktop.
Save nowshad-hasan/22904846eb2d00ccef86c92f4ae720ed to your computer and use it in GitHub Desktop.
// 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