Home >Java >javaTutorial >How to deal with duplicate method names in Java?

How to deal with duplicate method names in Java?

PHPz
PHPzOriginal
2023-06-25 16:50:362664browse

As an object-oriented programming language, Java's core ideas are encapsulation, inheritance and polymorphism. Duplication of method names is a common problem in Java, which usually occurs during inheritance and interface implementation. This article will introduce how to deal with duplicate method names in Java.

1. Method name overloading

In the same class, the method names can be the same, which is called method overloading (Overload). Method overloading means that multiple versions of the same method name are allowed to exist in the same class. Their parameter lists are different (different number of parameters or different parameter types), but the method return type can be the same or different. When calling a method, the compiler will determine which method should be called based on the type and number of parameters passed in.

For example, the following code defines two methods named "add", whose parameters are different in number and type:

public class MathUtil {
    public static int add(int x, int y) {
        return x + y;
    }
    
    public static double add(double x, double y) {
        return x + y;
    }
}

In this way, when the add method is called, the compiler The processor will automatically select the corresponding method to call based on the type and number of parameters passed in:

int result1 = MathUtil.add(1, 2); // 调用第一个add方法
double result2 = MathUtil.add(1.0, 2.0); // 调用第二个add方法

2. Method name override

In the inheritance process of Java, if the subclass defines and If there is a method with the same name in the parent class, the subclass will override the method with the same name in the parent class. This situation is called method override.

The principle of overwriting is: the method in the subclass must have the same return type, method name and parameter list as the method in the parent class (the number and type of parameters are the same). In addition, when a subclass overrides a method of a parent class, the access rights cannot be less than the access rights of the method in the parent class.

For example, the following code defines a method named "print", which exists in both the parent class and the subclass:

public class Animal {
    public void print() {
        System.out.println("This is an animal.");
    }
}

public class Cat extends Animal {
    @Override
    public void print() {
        System.out.println("This is a cat.");
    }
}

When we call the print method of the Cat instance , will output "This is a cat." because the Cat class overrides the print method in the Animal class.

3. Duplicate method names in interfaces

In Java interfaces, you can define multiple methods with the same method name, but the parameter types and numbers must be different. This is called method differentiation. Repeating.

For example, the following code defines an interface named "f", which defines three methods with the same method name, but different parameters:

public interface Example {
    void f(int x);
    void f(double x);
    void f(String s);
}

Implement this interface The class must implement these three methods. For example, the following code implements the Example interface:

public class MyExample implements Example {
    @Override
    public void f(int x) {
        System.out.println("f(int): " + x);
    }

    @Override
    public void f(double x) {
        System.out.println("f(double): " + x);
    }

    @Override
    public void f(String s) {
        System.out.println("f(String): " + s);
    }
}

In this way, when calling the f method of the MyExample instance, the compiler will automatically select the corresponding method to call based on the type and number of parameters passed in. For example:

Example example = new MyExample();
example.f(123); // 调用MyExample中的f(int)方法
example.f(456.0); // 调用MyExample中的f(double)方法
example.f("Hello"); // 调用MyExample中的f(String)方法

4. Summary

Duplication of method names is a common problem in Java. There are three solutions: method overloading, method overwriting and method duplication. When writing code, we have to choose different methods to solve it according to the actual situation. At the same time, we also need to pay attention to following Java's method naming convention. The naming convention is clear and concise, which helps to improve the readability and maintainability of the code.

The above is the detailed content of How to deal with duplicate method names in Java?. For more information, please follow other related articles on the PHP Chinese website!

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