Home  >  Article  >  Java  >  Detailed explanation and examples of java overload and override

Detailed explanation and examples of java overload and override

高洛峰
高洛峰Original
2017-01-21 16:56:221806browse

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

java 重载(overload)与重写(override)详解及实例

Let me give you my definition first:

Overload: In the same class or classes with inheritance relationships, a group of names have the same name. Method groups with different parameters. The essence is the name given to different methods.

Override: Between two classes that have an inheritance relationship, the methods that exist in the parent class are redefined in the subclass. The essence is to provide different implementations for the same method.

Let’s first look at an example of overloading:

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(“我是重载的方法吗?”);
 
  }
 
}
public class OverloadChildextends OverloadParent {
 
  public void getPerson(double money){
 
     Sytem.out.println(“我是重载的方法吗”);
 
  }
 
}

Note:

(1) There are 4 methods with the same name in OverloadParent

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

(3) Method 4 and method 1 only have different return values, which do not constitute overloading, and the compiler will not pass it .

                                                     The return value is the result of method execution. When we call the method, we will not specify "I want to call a method with a return value of type xxx". It cannot become a feature of method overloading.

(4) OverloadParent inherits Demo, and it has all the methods in Demo. It felt that the existing method could not meet the needs, so it simply overloaded one.

Overloading flag: 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 look at an override example:

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

What will be output when executing the main method of OverrideChild?

The answer is: I can'tfly, 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) There is an @overwrite annotation before the fly method of the subclass, which appears in jdk1.5, only use For class inheritance, 1.6 can be used for interface implementation. This annotation helps the compiler check, and it can be omitted.​​​​

Override flag: The child inherits the parent class and has different implementations of the same method.

Application Scenario

Overloading: When the methods have similar functions but need to pass different parameters.

Overriding: When a subclass has its own unique behavior and inherits it from the parent class but cannot meet its own needs.

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

Thank you for reading, I hope to help everyone, thank you for your support for this site!

For more detailed explanations and examples of java overload and override, please pay attention to 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