Sealing Classes Flashcards

1
Q

Defining a sealed class

public sealed class Bear permits Kodiak, Panda {}
public final class Kodiak extends Bear {}
pubilc non-sealed class Panda extends bear {}
A
  • A sealed class is a class that restricts which other classes may directly extend it.

Sealed Class Keywords
* sealed: Indicates that a class or interface may only be extended/implemented by named classes or interfaces
* permits: Used with the sealed keyword to list the classes and interfaces allowed
* non-sealed: Applied to a class or interface that extends a sealed class, indicating that it can be extended by unspecified classes

does not compile because the class and sealed modifiers are in the wrong order.
public class sealed Frog permits GlassFrog {} // DOES NOT COMPILE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Seal class rules

A
  • A sealed class needs to be declared (and compiled) in the same package as its direct subclasses.
  • direct subclasses need to extend the sealed class
  • Every class that directly extends a sealed class must specify exactly one of the following three modifiers: final, sealed, or non-sealed.
  • interfaces are implicitly abstract and cannot be marked final. For this reason, interfaces that extend a sealed interface can only be marked sealed or non-sealed. They cannot be marked final.
  • Omitting the permits Clause
    • direct subclasses in the same file
// Snake.java
public sealed class Snake {}
final class Cobra extends Snake {}
* nested subclasses
public sealed class Snake permits Snake.Cobra {
final class Cobra extends Snake {}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Sealing Interfaces

A
  • the sealed interface must appear in the same package or named module as the classes or interfaces that directly extend or implement it.
  • One distinct feature of a sealed interface is that the permits list can apply to a class that
    implements the interface or an interface that extends the interface.
// Sealed interface
public sealed interface Swims permits Duck, Swan, Floats {}
// Classes permitted to implement sealed interface
public final class Duck implements Swims {}
public final class Swan implements Swims {}
// Interface permitted to extend sealed interface
public non-sealed interface Floats extends Swims {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Sealed Class Rules

A
  • Sealed classes are declared with the sealed and permits modifiers.
  • Sealed classes must be declared in the same package or named module as their direct subclasses.
  • Direct subclasses of sealed classes must be marked final, sealed, or non-sealed.
  • The permits clause is optional if the sealed class and its direct subclasses are declared within the same file or the subclasses are nested within the sealed class.
  • Interfaces can be sealed to limit the classes that implement them or the interfaces that extend them.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly