Home Java Javagetting Started The difference between overloading and rewriting in java

The difference between overloading and rewriting in java

Nov 11, 2019 am 09:30 AM
java

The difference between overloading and rewriting in java

##Override (Recommended learning: java course)

Literally, override is Meaning to write it again. In fact, it is to rewrite the methods of the parent class itself in the subclass. The subclass inherits the original method of the parent class, but sometimes the subclass does not want to inherit a method in the parent class unchanged, so the method name, parameter list, return type (except the return value of the method in the subclass When the return value of the method in the parent class is the same as the subclass), modifying or rewriting the method body is called rewriting. However, it should be noted that the access modification permissions of subclass functions cannot be less than those of the parent class.

For example:

public class Father {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Son s = new Son();
        s.sayHello();
    }
    public void sayHello() {
        System.out.println("Hello");
    }
}
class Son extends Father{
    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("hello by ");
    }

}
Copy after login

Overload

In a class, if the method with the same name has different Parameter lists (different parameter types, different number of parameters, or even different parameter order) are considered overloads. At the same time, overloading has no requirement for return types, which can be the same or different, but overloading cannot be judged by whether the return types are the same.

For example:

public class Father {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Father s = new Father();
        s.sayHello();
        s.sayHello("wintershii");
    }

    public void sayHello() {
        System.out.println("Hello");
    }
    public void sayHello(String name) {
        System.out.println("Hello" + " " + name);
    }
}
Copy after login

During the interview, asked: What is the difference between overload and override?

Answer:

Overloading and rewriting of methods are both ways to achieve polymorphism. The difference is that the former implements compile-time polymorphism, while the latter implements run-time polymorphism. Polymorphism.

Overloading occurs in a class. If a method with the same name has a different parameter list (different parameter types, different number of parameters, or both), it is considered an overload; overriding occurs. Between the subclass and the parent class, overriding requires that the overridden method of the subclass has the same parameter list as the overridden method of the parent class, has a compatible return type, is easier to access than the overridden method of the parent class, and cannot be compared to the overridden method of the parent class. The overridden method of the parent class declares more exceptions (Liskov substitution principle).

Overloading has no special requirements for the return type and cannot be distinguished based on the return type.

The above is the detailed content of The difference between overloading and rewriting in java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Square Root in Java

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Perfect Number in Java

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Random Number Generator in Java

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Armstrong Number in Java

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Weka in Java

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Smith Number in Java

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

Java Spring Interview Questions

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

See all articles