Home  >  Article  >  Java  >  How to use Sealed Classes to restrict class inheritance in Java 14

How to use Sealed Classes to restrict class inheritance in Java 14

王林
王林Original
2023-07-29 19:42:22580browse

How to use Sealed Classes to restrict class inheritance relationships in Java 14

In Java 14, the concept of Sealed Classes was introduced, which provides a mechanism to restrict class inheritance relationships. Sealed Classes allow us to explicitly specify which classes can inherit from this class, and which classes are allowed to become direct subclasses of this class. In this way, we can better control the inheritance relationship of classes and reduce the potential problems of inheriting a class that should not be inherited.

In this article, we will introduce how to use Sealed Classes in Java 14 and provide some code examples to help understanding.

First, let us create a base class, assuming we are building a class hierarchy for an electronic device:

public abstract sealed class ElectronicDevice permits Laptop, Desktop {

    // 基类的代码...

}

In the definition of the base class, we use two keywords: abstract and sealed. abstract is used to indicate that the class is an abstract class and cannot be instantiated; sealed is used to indicate that the class is a Sealed Class, which limits the inheritance scope of its subclasses.

Next, we create two subclasses: Laptop and Desktop, and make them inherit from ElectronicDevice:

public final class Laptop extends ElectronicDevice {

    // Laptop类的代码...

}

public non-sealed class Desktop extends ElectronicDevice {

    // Desktop类的代码...

}

In the definition of subclasses, we use the keywords final and non-sealed. final means that the class cannot be inherited, non-sealed means that the class is a subclass of ElectronicDevice, but is not a Sealed Class. This means that, except for the two classes Laptop and Desktop, other classes cannot directly inherit from ElectronicDevice.

Next, we can define some methods and properties in the ElectronicDevice class. For Sealed Class, due to its restricted inheritance relationship, you can use these methods and properties in your code with more confidence without worrying that other subclasses that should not inherit the class may destroy the invariance of the class or introduce inconsistencies. .

In addition, Sealed Class also provides a mechanism to clearly allow which classes can become subclasses of this class when defining a subclass. For example, we can create a subclass named Smartphone and allow only two classes, IOSPhone and AndroidPhone, to inherit:

public sealed class Smartphone extends ElectronicDevice permits IOSPhone, AndroidPhone {

    // Smartphone类的代码...

}

public final class IOSPhone extends Smartphone {

    // IOSPhone类的代码...

}

public final class AndroidPhone extends Smartphone {

    // AndroidPhone类的代码...

}

In this example, we use the permits keyword to explicitly specify the scope of classes that the Smartphone class is allowed to inherit from. This means that only two classes, IOSPhone and AndroidPhone, can inherit from Smartphone, but other classes cannot.

Through Sealed Classes, we can better manage and restrict the inheritance relationship of classes and improve the readability and maintainability of the code. You only need to use the sealed keyword when defining a class and the permits keyword when defining a subclass to clearly define the inheritance scope of the class and avoid unexpected inheritance relationships that may lead to potential The problem.

To summarize, in Java 14, Sealed Classes provides a more flexible and precise class inheritance relationship management mechanism. Through the reasonable use of Sealed Classes, we can better control and protect the inheritance relationship of classes, thereby improving the reliability and maintainability of the code.

I hope this article will help you use Sealed Classes to restrict class inheritance relationships in Java 14. Thanks for reading!

The above is the detailed content of How to use Sealed Classes to restrict class inheritance in Java 14. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn