Home  >  Article  >  Backend Development  >  final keyword

final keyword

WBOY
WBOYOriginal
2016-07-29 08:51:131422browse

The Chinese meaning of final is final and final. Classes and methods modified by the keyword final are the "final classes and methods". In other words, classes modified with the final keyword cannot be inherited, and methods modified with the final keyword cannot be overridden.

If there is a class in the format:

final class MyClass{

//...

}

, it means that other classes can no longer use the extends keyword to inherit this class.

If there is a method modified with the final keyword, as shown below:

final function method_name()

It means that this method cannot be overridden in subclasses of the class where it is located.

For example:

Generate a subclass ChildClass for the final class MyClass. You can see that the program reports an error and cannot be executed. The code is as follows:



The result is:

Fatal error: Class ChildClass may not inherit from final class (MyClass) in D:wampserverwwwtesttestindex1.php on line 17

The above has introduced the final keyword, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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