Final class and Final method

end of inheritance

Final class

       When the keyword final is used to modify a class, its meaning is that the class cannot be derived from subclasses. In other words, no other class can inherit a class modified with final, even if the access of the class is restricted to public types, it cannot be inherited; otherwise, a compilation error will be reported. Other classes that can be inherited.

      When should a class be finalized?

     Final classes should be created only when it is necessary to ensure that all methods in the class are not overridden, the final keyword will provide safety for these methods, and no one can override methods in final classes because they cannot be inherited.

 

Final method

      When a method is modified with the final keyword, the method cannot be overridden in subclasses, but can only be inherited.

      To properly use a final method, only when the subclass overrides a method will cause problems, and then set the method as a final method. Generally, it is unnecessary to use it. Because preventing subclass overrides loses some of the object-oriented benefits, including the extensibility achieved through overrides.

 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325363155&siteId=291194637