About Question enthuware.ocpjp.v17.2.3687 :
Posted: Tue Oct 08, 2024 6:31 pm
Given the following code:
interface Readable{ } non-sealed class Device implements Readable { }
sealed class DocType implements Readable permits Book, Journal { }
final non-sealed class Book extends DocType{ }
final class Journal extends DocType{ }
Which types will fail compilation?
Book
Non-sealed implies that a class may have subclasses, while final implies that a class cannot be subclassed. Both are contradictory. That is why, a class cannot be both - final and non-sealed, at the same time.
Why is this answer correct?
interface Readable{ } non-sealed class Device implements Readable { }
sealed class DocType implements Readable permits Book, Journal { }
final non-sealed class Book extends DocType{ }
final class Journal extends DocType{ }
Which types will fail compilation?
Book
Non-sealed implies that a class may have subclasses, while final implies that a class cannot be subclassed. Both are contradictory. That is why, a class cannot be both - final and non-sealed, at the same time.
Why is this answer correct?