Sealed Classes Flashcards

1
Q

How do we define sealed classes?

A

public sealed class Name permits SomeClass {}

Each class that is permitted must extend class that permits it:

public final class SomeClass extends Name {}

Instead of “final”, there can be “sealed” and “non-sealed”

Both classes need to be in the same package.

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

What is the purpose of “final” in classes that extend some sealed class?

A

It is there to block that class of being extended.

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

What is the purpose of “non-sealed” in classes that extend some sealed class?

A

It opens sealed classes to other classes.

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

When can “permits” be omitted?

A

When class that would be permitted is in the same java file, nested class or in same named module.

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

How do we define sealed interfaces?

A

All rules apply as to sealed classes, but difference is that interfaces can permit other interfaces and classes

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

Can sealed classes permit interfaces?

A

No, because interfaces don’t implement classes.

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