Override and overload

The overriding of Java methods is based on the inheritance relationship. The method of the subclass rewriting the parent class has the same method name, number of parameters, type, and return value. If you must call the method of the parent class, you can use the super keyword. By default is to call the overridden method of the subclass

 

The access rights of the subclass to override the method of the superclass cannot be stricter than that of the superclass. For example, if the access rights of the superclass method are private, it can only be accessed by the superclass, and the subclass cannot be overridden.

 

Cannot be accessed by external classes and cannot be overridden in subclasses. If the method of defining the parent class is public, and the subclass is defined as private, an error will be reported when the program runs.

 

(5) If a method in the parent class throws an exception during the inheritance process, then when the method of the parent class is rewritten in the subclass, an exception should also be thrown,

 

And the exception thrown cannot be more than the exception thrown in the parent class (can be equal to the exception thrown in the parent class). In other words, the overridden method must not throw a new checked exception

 

Or a checked exception that is more general than the overridden method declaration. For example, a method of the parent class declares a checked exception IOException, and cannot throw Exception when overriding this method.

 

Only subclasses of IOException can be thrown, and unchecked exceptions can be thrown. In the same way, if a member variable is created in the subclass

Guess you like

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