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

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

WBOY
WBOYOriginal
2023-07-30 23:08:051293browse

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

With the continuous development of Java, new versions are continuously launched, providing developers with more powerful functions and tools to improve Code readability and maintainability. In Java 14, the concept of Sealed Classes was introduced, a mechanism that restricts the inheritance and implementation of classes. This article will introduce in detail how to use Sealed Classes and its practical application scenarios.

  1. What are Sealed Classes?
    Sealed Classes is a mechanism that limits the inheritance and implementation of classes. By adding the keyword sealed before the definition of a class or interface, you can define a range of subclasses or implementation classes of that class or interface. Only subclasses or implementation classes within this scope can inherit or implement the class or interface, while classes outside the scope cannot inherit or implement it. This can avoid the abuse or misuse of classes and improve the security and stability of the code.
  2. How to use Sealed Classes
    Before defining a Sealed Class, you need to define a Sealed Interface or an ordinary abstract class. This Sealed Interface or abstract class plays a role in limiting the scope and defines which classes can inherit or implement the class or interface. Next, you can use the sealed keyword to define the class or interface. The code example is as follows:
public sealed class Animal permits Cat, Dog, Bird {
    // class body
}

public final class Cat extends Animal {
    // class body
}

public final class Dog extends Animal {
    // class body
}

public final class Bird extends Animal {
    // class body
}

public class InvalidClass extends Animal {
    // compile error: InvalidClass is not allowed to extend Animal
}

In the above code, a Sealed Class Animal is defined. And through the permits keyword, the subclasses of this class are defined as Cat, Dog and Bird, while the class InvalidClass exceeds the scope and cannot inherit the Animal class.

  1. Characteristics and limitations of Sealed Classes
  2. Sealed Classes can be a final class, that is, subclass extensions are not allowed.
  3. Sealed Classes can contain abstract methods, but methods with default implementation are not allowed.
  4. When Sealed Classes are defined as non-final classes, but subclasses are not specified, all direct subclasses must be declared in the same source file.
  5. Subclasses of Sealed Classes must be defined in the same package as the parent class.
  6. Indirect subclasses of Sealed Classes must be final classes.
  7. Practical application scenarios of Sealed Classes
    Sealed Classes has many application scenarios in actual development. Here are some examples:
  • In state machine mode In Sealed Classes, you can use Sealed Classes to define a limited set of states and limit the inheritance and implementation of the states to ensure that only the specified states can be used.

    public sealed class State permits Waiting, Running, Closed {
      // class body
    }
    
    public final class Waiting extends State {
      // class body
    }
    
    public final class Running extends State {
      // class body
    }
    
    public final class Closed extends State {
      // class body
    }
    
    public final class InvalidState extends State {
      // compile error: InvalidState is not allowed to extend State
    }
  • In the development of frameworks and libraries, Sealed Classes can be used to restrict the inheritance and implementation of certain classes to ensure that the core logic of the framework will not be changed.

    public sealed class CoreLibrary permits Util, Helper, Logger {
      // class body
    }
    
    public final class Util extends CoreLibrary {
      // class body
    }
    
    public final class Helper extends CoreLibrary {
      // class body
    }
    
    public final class Logger extends CoreLibrary {
      // class body
    }
    
    public final class InvalidLibrary extends CoreLibrary {
      // compile error: InvalidLibrary is not allowed to extend CoreLibrary
    }

As you can see from the above examples, Sealed Classes brings more control over the inheritance and implementation of classes to Java developers, which can improve the security and stability of the code. Avoid the abuse and misuse of classes. However, it should be noted that Sealed Classes are not suitable for all scenarios and still need to be used with caution based on actual needs.

Summary:
This article introduces how to use Sealed Classes in Java 14 to restrict class inheritance and implementation. By using the sealed keyword and defining the permits keyword, you can limit the scope of the class and avoid the abuse and misuse of the class. At the same time, we also discussed the characteristics and limitations of Sealed Classes, as well as practical application scenarios. In actual development, reasonable use of Sealed Classes can improve the security and stability of the code, making the code easier to understand and maintain.

The above is the detailed content of How to use Sealed Classes to restrict class inheritance and implementation 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