Home  >  Article  >  Java  >  What classes in Java cannot be inherited

What classes in Java cannot be inherited

(*-*)浩
(*-*)浩Original
2019-05-21 17:57:546819browse

There are some classes in java that cannot be inherited. We can design some read-only immutable classes through these classes that cannot be inherited. What are the classes in Java that cannot be inherited?

What classes in Java cannot be inherited

In Java, as long as a class is defined as final, it can also be said to be final Modified classes cannot be inherited.

This is the final rule:

当你将final用于类身上时,一个final类是无法被任何人继承的,那也就意味着此类在一个继承树中是一个叶子类,并且此类的设计已被认为很完美而不需要
进行修改或扩展。对于final类中的成员,你可以定义其为final,也可以不是final。而对于方法,由于所属类为final的关系,自然也就成了final型的。你也可以明确的给final
类中的方法加上一个final,但这显然没有意义。

For example: the String class we use is also a class that cannot be inherited.

Then let’s take a look at this problem together. First of all, we need to know that if you want to determine whether a class can be inherited, you need to know how this class is defined, so we first Let’s take a look at how this String class is defined:

What classes in Java cannot be inherited

#The above is how this String class is defined. We can clearly see a final modifier!

If you say that the entire class is final (prefix it with the final keyword before its definition), it means that you do not want to inherit from this class, or do not allow anyone else to take this action. In other words, for one reason or another, our class definitely does not need any changes; or for security reasons, we do not want to subclass (subclass processing)

The above is the detailed content of What classes in Java cannot be inherited. 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
Previous article:what is java identifierNext article:what is java identifier