search
HomeJavajavaTutorialOverloading and overriding of JAVA methods

Overloading of methods: (used to implement the same function, but requires different parameters to satisfy different users) in the same class.

means that multiple methods with the same name, the same or different return values, but different parameters (number of parameters or parameter types) can be defined in a class. When calling, use overloading, which is simple and convenient for users to use the API. There is no need to give different names to methods with the same function, which is convenient for users. For users, doing the same thing is like calling the same function. Constructors can also be overloaded. As long as the program defines its own construction method, the system does not provide a default construction method.

Overriding of methods (rewriting): (Subclasses override the methods of parent classes, in different classes)

Overriding of methods (override/ overwrite)

Difference from overloading: Method overloading is to define multiple functions in a class with the same name, the same return type, but different parameters

When overwriting a method: It is best to use Copy this function of the parent class

1. In the subclass, you can rewrite the methods inherited from the base class as needed.

2. The overridden method must have the same method name, parameter list and return type as the overridden method.

3. Overriding methods cannot use more restrictive access rights than the overridden method.

Strict order private>default>protecte>public, this is related to polymorphism. You will understand it much more when you talk about polymorphism. When a certain place is modified by the parent class, a method of the parent class can be called. If the subclass has stricter access to this method, when passing the subclass, it may become inaccessible

Polymorphism of java: overloading + overwriting

Question: What mechanism is used in Java Implement polymorphism?

Polymorphism is one of the object-oriented features of Java. Different manifestations of polymorphism are achieved in Java through overloading and overwriting. But there is a difference between the two.

1) Overloading

Overloading of methods in Java refers to the fact that multiple methods can be created in a class. They have the same name but different argument columns. (parameter type and number) and different definitions. The types of return values ​​can be the same or different, but overloaded functions cannot be distinguished just by different return values. When calling a method, the parameter list is used to determine which method to call. Overloading is a manifestation of polymorphism in a class.

2) Overriding

In Java, subclasses can inherit methods from parent classes by default without rewriting the same methods. However, sometimes, subclasses don’t want to Inheriting the methods from the parent class unchanged, but making certain modifications, is achieved by overriding the method (also called overwriting). Overwriting in Java refers to changing the implementation part of the same function with the same name in the parent class in the subclass, but keeping the method name, return type and parameter list of the method in the parent class consistent. That is, the subclass redefines the functions in the parent class, and the new methods in the subclass will overwrite the original methods of the parent class. Overriding is a polymorphic expression between parent class and subclass.

The above is the detailed content of Overloading and overriding of JAVA methods. 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 do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.