English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation and Examples of Overloading (Overload) and Overriding (Override) in Java

 Many students are confused about the concepts of overload and override, and it is recommended not to memorize conceptual knowledge by rote, but to understand and memorize it.

  

    First, give my definition:

    overload (overloading): A group of methods with the same name but different parameters in the same class or classes with inheritance relationships. In essence, it is a way to refer to different methods.

    override (override): In the two classes with inheritance relationships, a method that exists in the parent class is redefined in the subclass. In essence, it is to provide different implementations for the same method.

 Let's take a look at an example of overloading first:

public class OverloadParent{
  public String getPerson(String name){
    return "personA" + name;
  }
  public String getPerson(int age){
    return "personB" ;
  }
  public String getPerson(String name, int age){
    return "personC";
  }
  public void getPerson(String name){
    System.out.println("I am an overloaded method?");
  }
}
public class OverloadChild extends OverloadParent {
  public void getPerson(double money){
     Sytem.out.println("I am an overloaded method?");
  }
}

    Notice the observation:

    (1) OverloadParent has4methods with the same name

    (2) The parameter types and numbers of the first three methods are different, and the return values are consistent, constituting overloading

    (3) method4With the method1Just the return value is different, it does not constitute overloading, and the compiler will not pass it.

        ps: The return value is the result of the method execution. We do not specify when calling the method that 'I want to call a method whose return value is of type xxx', it does not become a feature of method overloading.

    (4)OverloadParent inherits from Demo, and has all the methods that Demo has. It feels that the existing methods do not meet the needs, so it simply overloads one.

     The sign of overloading: The method name is the same, the parameters are different (number or type), and it has nothing to do with the return value.

     Let's take another example of overriding:

  public class OverrideParent{
     public void fly(){
       System.out.println("I can fly!");
    }
  }
  public class OverrideChild extends OverrideParent{
    @Override
    public void fly(){
         System.out.println("I can't fly, but I can run!");
    }
   public static void main(String[] args){
         OverwriteParent child = new OverwriteChild();
         child.fly();
    }
  }

    What will be output when the main method of OverrideChild is executed?

    The answer is: I can't fly, but I can run!

    We see:

    (1)Both OverrideChild and OverrideParent have a fly method

    (2)The return value and modifier of fly are the same, only the method body is different

    (3)The fly method of the subclass has a @overwrite annotation, jdk1.5Only used for class inheritance,1.6Can be used for interface implementation. This annotation helps the compiler check, but it is not necessary to add it.       

The sign of overriding: Subclass inherits from superclass and has different implementations for the same method. 

Application Scenarios

         Overloading: When a method has similar functions but requires different parameters.

         Overriding: When a subclass has its own unique behavior and cannot meet its own needs by inheriting from the superclass.

         ps: Overloading and overriding are both manifestations of polymorphism. The former is compiler polymorphism, and the latter is runtime polymorphism.

          Thank you for reading, I hope it can help everyone, thank you for your support to our website!

You May Also Like