Java Interview Question 2: The difference between overloading and rewriting

1. Overload:

        In Java, method overloading is to create multiple methods with the same name in a class. The parameter types and the number of parameters in the method are different. Multiple functions with the same name exist at the same time. Overloading is a manifestation of polymorphism. When calling methods, the number of parameters and parameter types passed to them are used to determine which method to use. The return value type of the method can be the same or different. Java overloading is judged based on the type and number of parameters, and has nothing to do with the return value of the method.

       Features: In the same class, the method name is the same, the parameter type and the number of parameters are different. Can have different access modifiers; can throw different exceptions;

       The code example is as follows:

 public class Dog {
        
         void bark()
         {
                System.out.println(\"no barking!\");
                this.bark(\"female\", 3.4);
         }
         void bark(String m,double l)
         {
                System.out.println(\"a barking dog!\");
                this.bark(5, \"China\");
         }
}
         

2. Rewrite (overwrite):

        In Java, rewriting refers to the rewriting of the method in the parent class by the method of the inheritance relationship. If a method defined in a subclass has the same name and parameters as its parent class, the method is overridden. In Java, subclasses can inherit the methods in the parent class without writing the same method. But sometimes the subclass wants to make changes to the method in the inherited parent class, so the method rewrite occurs.

       Features: The method in the subclass and the method in the parent class have the same method name, return type, and parameter type. The overridden method in the subclass will overwrite the original parent method, and the access modifier must be more restrictive than the access modifier of the overridden method (public>protected>default>private).

                 The return type must always be the same as the return type of the method being overridden. The overridden method must not throw new checked exceptions or checked exceptions that are broader than the overridden method declaration. For example: a method of the parent class declares a check exception IOException. When this method is overridden, Exception cannot be thrown, only the subclass exception of IOException can be thrown. Ability to throw unchecked exceptions.

 

3. Summary

    Simply put, the same method can be different depending on the input data. Make different treatments, that is,
    overloading of methods -with different parameter lists (static polymorphism)

  And when the child class inherits the same method from the parent class. The input data is the same. But to make a response different from the parent class, you have to override the parent class method,

    That is to rewrite the method in the subclass-the same parameters, different implementations (dynamic polymorphism)

 

 

Guess you like

Origin blog.csdn.net/Sunshineoe/article/details/114604724