[Interview Questions] What is the difference between Java overloading and rewriting?

Sometimes the blog content will change. The first blog is the latest, and other blog addresses may not be synchronized. Check it carefully.https://blog.zysicyj.top

First blog address [1]

Interview question manual [2]

Series article address [3]


1. What are overloading and rewriting?

  • Overloading means that in the same class, multiple methods with the same method name but different parameter lists can be defined. These methods are distinguished by the type, order, or number of arguments.
  • Overriding means that a subclass reimplements an existing method in the parent class. Subclasses inherit the methods of the parent class and modify or supplement them.

2. Why do we need to overload and rewrite?

  • Overloading can provide a more flexible method calling method, making the code more concise and readable. For example, you can use different parameter types to handle different situations without having to write separate methods for each situation.
  • Overriding allows a subclass to change or extend the behavior of a parent class according to its own specific needs. This enables polymorphism, i.e. calling methods of child class objects through parent class references.

3. How is overloading implemented?

  • In Java, overloading is a form of static binding (also called early binding). The compiler determines the specific method to call based on the method name and parameter list. Therefore, it is possible to determine which method to call at compile time.

4. Examples of overloading

public class Calculator {
    
    
    public int add(int a, int b) {
        return a + b;
    }

    public double add(double a, double b) {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        int result1 = calculator.add(23);
        double result2 = calculator.add(2.53.7);
        System.out.println(result1); // 输出:5
        System.out.println(result2); // 输出:6.2
    }
}

In the above example, Calculatorthe class defines two addmethods named , one that accepts two integer parameters and the other that accepts two floating point parameters. Through overloading, we can call appropriate methods based on different parameter types.

5. Advantages of overloading

  • Provides a more flexible method calling method, making the code more concise and readable.
  • Can handle parameters of different types, orders or numbers, improving code reusability and scalability.

6. 重载的缺点

  • 当存在多个重载方法时,容易造成混淆和误解。因此,在设计重载方法时需要注意命名规范和参数选择,避免产生歧义。

7. 重载的使用注意事项

  • 重载方法必须具有不同的参数列表(参数类型、顺序或数量)。
  • 返回值类型对于重载没有影响。
  • 重载方法不能仅通过访问修饰符、返回类型或抛出异常进行区分。

8. 总结

  • 重载 是指在同一个类中定义多个方法名相同但参数列表不同的方法,通过静态绑定实现。
  • 重写 是子类重新实现父类已有方法的过程,通过动态绑定实现。
  • 重载提供了更灵活的方法调用方式,而重写允许子类改变或扩展父类的行为。

参考资料

[1]

首发博客地址: https://blog.zysicyj.top/

[2]

面试题手册: https://store.amazingmemo.com/chapterDetail/1685324709017001

[3]

系列文章地址: https://blog.zysicyj.top/categories/技术文章/后端技术/系列文章/面试题精讲/

本文由 mdnice 多平台发布

Guess you like

Origin blog.csdn.net/njpkhuan/article/details/133552156