Difference between method overriding and overloading in Java

The difference between Java method override (Override) and overload (Overload) (super detailed)

HomeBefore we want to learn this knowledge point, we should first understand what is polymorphism?

When first learning java, people all know that java, an object-oriented language, has three characteristics: encapsulation, inheritance, and polymorphism.

Polymorphism is the ability to have multiple different manifestations or forms of the same behavior.

For example, most animals (abstract classes) can bark, but dogs (implementation classes) are barking, and cats (implementation classes) are meowing.

Necessary conditions for polymorphic implementation

  1. Subclass must inherit parent class
  2. rewrite
  3. Parent class reference points to child class object

How to implement polymorphism

There are three ways to implement polymorphism: rewriting, overloading, and interface implementation . Although their implementation methods are different, the core is: different manifestations of the same behavior.

After a general understanding of what polymorphism is, let's take a look at Java method Override and Overload

1. Override

  1. The polymorphic relationship between the parent class and the child class redefines the functions of the parent class. If some method defined in the subclass has the same name and parameters as the superclass, then this is the method being overridden.
    But sometimes the subclass does not want to inherit the method of the parent class, and wants to make some changes, which requires method rewriting.
    The rewriting of the method is reflected in the sub-parent class. The most typical is that the sub-class rewrites the method in the parent class. The
    rewriting code is as follows:
public class Father {
    
    
        public void walk(){
    
    
            System.out.println("我是父亲");
      }

}

public class Son extends Father {
    
    
    @Override//方法重写
    public void walk() {
    
    
        System.out.println("我是儿子");
    }
}
  1. Method overriding is also known as method overriding.
    If a subclass method has the same method name, return type, and parameter list as a method in the superclass, the new method will override the old one.
  2. The access modification rights of the subclass method cannot be less than that of the superclass.

2. Overload

  1. Multiple methods with the same name exist at the same time, with different parameters/types. Overloading is a manifestation of polymorphism in a class
  2. When overloading, the method name must be the same, but the parameter type and number are different, and the return value type can be the same or different . We can't use the return value type as a distinguishing criterion for overloaded methods! ! ! If the parameter type is the same as the number, and the return value type is different, it is not overloaded .
    This way of writing will directly report an error. Because we don't know the return value type of the method when the method is called, the compiler cannot distinguish you. Which method is called.
  3. When calling methods, it is determined which corresponding method to use by passing them different number of parameters and parameter types. This is polymorphism.
    The overload code is as follows:
public void eat(){
    
    
        System.out.println("我是干饭人" );
    }

//    public int eat(){  会报错
//        return 4;
//    }
    //报错原因:参数类型和个数一样,返回值类型不同是不算重载的
    //因为在调用方法的时候,我们还不知道方法的返回值类型,所以编译器无法区分你调用的是哪个方法。


    public void eat(String name){
    
    
        System.out.println("我是干饭人:"+name );
    }

    public void eat(String name,int age){
    
    
        System.out.println("我是干饭人:"+name+"我今年"+age );
    }

Note here:
Overriding Override is a polymorphic relationship between parent classes and subclasses , while overloading Overload ismanifestation of polymorphism in a class .

3. Summary

Method overloading Overload:

1. In the same class

2. The method name is the same, but the parameter list is different (parameter order, number, type)

3. Method return values, access modifiers are arbitrary

4. It has nothing to do with the parameter name of the method

The method overrides Override:

1. In the subclass with inheritance relationship

2. The method name is the same, the parameter list is the same (parameter order, number, type), and the method return value is the same

3. Access modifier, the access range needs to be greater than or equal to the access range of the parent class

4. It has nothing to do with the parameter name of the method

Guess you like

Origin blog.csdn.net/weixin_46015018/article/details/122600829