Home > Article > Backend Development > Notes on overriding functions: Avoiding minefields in inheritance
Five precautions need to be followed when rewriting functions: 1. Keep parameters and return types consistent; 2. Use @Override annotation; 3. Avoid overriding final methods; 4. Control access permissions; 5. Fully understand and test the parent class method.
Notes on overriding functions: Avoiding traps in inheritance
In object-oriented programming, overriding functions is a A key technology that allows subclasses to modify the behavior of methods in parent classes. However, when rewriting functions, care must be taken to avoid potential pitfalls.
1. Make sure the parameters and return types are consistent
The rewritten function must have the same parameters and return types as the parent class method. Any changes will cause compile-time errors or run-time exceptions.
Java code example:
class Parent { int add(int a, int b) { ... } } class Child extends Parent { // 重写 add 方法,但修改了参数类型 int add(float a, float b) { ... } // 编译错误 }
2. Use the @Override annotation
When rewriting functions, it is recommended to use @Override
Annotation. It ensures that you are overriding the parent class's methods and not accidentally creating new methods.
Java code example:
class Child extends Parent { @Override int add(int a, int b) { ... } }
3. Avoid overriding final methods
final
Methods cannot was rewritten. Attempting to do this will result in a compile-time error.
Java code example:
class Parent { final int add(int a, int b) { ... } } class Child extends Parent { // 尝试重写 final 的 add 方法 int add(int a, int b) { ... } // 编译错误 }
4. Handle access permissions carefully
Access permissions for methods overridden by subclasses Access permissions cannot be more restrictive than those of parent class methods.
Java code example:
class Parent { protected int add(int a, int b) { ... } } class Child extends Parent { // 尝试将访问权限收紧为 private private int add(int a, int b) { ... } // 编译错误 }
5. Test and understand parent class methods
Before overriding a function, please Carefully test and understand the expected behavior of parent class methods. The overridden method should provide the same or greater functionality as the parent class method.
Practical case
Consider a Vehicle
base class and its subclasses Car
and Bike
. The Vehicle
class has a start()
method for starting the vehicle.
Vehicle.java
class Vehicle { public void start() { System.out.println("Vehicle started."); } }
Car.java
class Car extends Vehicle { @Override public void start() { System.out.println("Car engine started."); super.start(); // 调用父类方法以启动常规车辆功能 } }
Bike.java
class Bike extends Vehicle { @Override public void start() { System.out.println("Bike pedaling started."); } }
In this example, Car
overrides the start()
method to add specific startup engine code, while Bike
overrides start ()
Method to provide pedal-related launch behavior. By correctly overriding functions, a subclass can customize the parent class's methods to meet its specific needs.
The above is the detailed content of Notes on overriding functions: Avoiding minefields in inheritance. For more information, please follow other related articles on the PHP Chinese website!