search
HomeJavajavaTutorialWhat is the difference between rewriting and overloading in java

Difference: Rewriting is when a subclass rewrites the implementation process of a method that allows access to the parent class. The method name and parameter list are the same, and the return value and formal parameters cannot be changed. Overloading is when multiple functions of the same name with different number or types of parameters exist in the same class at the same time, with the same method name and different parameter lists.

What is the difference between rewriting and overloading in java

Related recommendations: "Java Video Tutorial"

##Q: What are Java overloading and rewriting? What's the difference?

Answer:

 
Overload (Overload) is a means for a class to process different types of data in a unified way. Functions with the same name with different number or types of parameters (the return value type can be arbitrary, and the return type cannot be used as the criterion for distinguishing overloaded functions) exist in the same class at the same time, which is polymorphism in a class A manifestation of polymorphism (when calling a method, determines which method to use by passing different number of parameters and parameter types ).

What is the difference between rewriting and overloading in java

Figure 1. An overloading example in an Android class
What is the difference between rewriting and overloading in java
Override

(Override) is the parent class and The essence of polymorphism between subclasses is to redefine the functions of the parent class. If a method defined in a subclass has the same name and parameters as its parent class, the method will be rewritten, but the access of the subclass function The modification permission cannot be less than that of the parent class; if a method in the subclass has the same method name, return type and parameter list as a method in the parent class, the new method will overwrite the original method. If the original method in the parent class is required The method can use the super keyword.

What is the difference between rewriting and overloading in java

Figure 2. A common overriding example in Android classes
What is the difference between rewriting and overloading in java----------

** Overloading rules: ** must have different parameter lists; can have different return types; can have different access modifiers; can throw different exceptions.

Rewriting rules:

The parameter list must be exactly the same as the overridden method, otherwise it cannot be called an override; the return type must always be the same as the overridden method , otherwise it cannot be called an override; The access modifier limit must be greater than or equal to the access modifier of the overridden method; the overridden method must not throw a new checked exception or be broader than the overridden method declaration Checked exceptions, for example, the parent class method declares a checked exception IOException. When overriding this method, you cannot throw Exception. You can only throw subclass exceptions of IOException, and you can throw unchecked exceptions.

Overloading and rewriting are different manifestations of Java polymorphism.  
Rewriting is a manifestation of polymorphism between the parent class and the subclass, which works at runtime (dynamic polymorphism, such as dynamic binding)  
Overloading is a manifestation of polymorphism in a class, which takes effect at compile time (static polymorphism, such as static binding).


Q: Can Java constructors be overridden and overloaded? Answer:

Overriding is a method in which a subclass method overrides a parent class method. The overridden method name remains unchanged, and the class constructor method name must be consistent with the class name. Assuming that if the construction method of the parent class can be overridden by the subclass, the class name of the subclass must be consistent with the class name of the parent class, so

Java's construction method
cannot be overridden . The overloading is for the same one, so the construction method can be overloaded .

Q: What is the result of the following program and why?

public class Demo {
    public boolean equals( Demo other) {
        System.out.println("use Demo equals." );
        return true;    }
    public static void main(String[] args) {    
    Object o1 =new Demo ();
    Object o2 =new Demo ();        
    Demo o3 =new Demo ();
    Demo o4 =new Demo ();

   if (o1.equals(o2)) {
            System.out.println("o1 is equal with o2.");
        }

   if(o3.equals(o4)) {             
            System.out.println("o3 is equal with o4.");
        }
    }}
Answer: The results of the above program are as follows.

use Demo equals.
o3 is equal with o4.
Because the public boolean equals(Demo other) method in the Demo class does not override the public boolean equals(Object obj) method in the Object class, the reason is that it violates the parameter rules, one of which is the Demo type The other one is of type Object, so these two methods are in an overloading relationship (occurring at compile time) rather than an overriding relationship;

So when o1.equals(o2) is called, o2 is an Object type parameter, in fact The public boolean equals(Object obj) method

in the Object class is called, because at compile time o1 and o2 are both Object types, and the

equals method of the Object class returns false by comparing the memory address. ;When o3.equals(o4) is called, the equals(Demo other) method in the Demo class is actually called. Because o3 and o4 are both of Demo type at compile time, the above printing is there.

The above is the detailed content of What is the difference between rewriting and overloading in java. For more information, please follow other related articles on 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
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)