当前位置:网站首页>The difference between overloading and rewriting
The difference between overloading and rewriting
2022-07-26 07:53:00 【I like iced black tea】
List of articles
The difference between overloading and rewriting
One 、 heavy load
heavy load (overloading) It's in a class , The method name is the same , And the parameters are different . The return type can be the same or different .
Every overloaded method ( Or constructors ) There must be a unique list of parameter types .
The most common place is the overloading of construction methods .
Overloads are in the same class with the same method name , Different parameters ( Number , type , The order ) Definition of the method of ,
Construction method common method , Abstract methods can be overloaded ; Rewriting is when a subclass inherits from its parent class ,
The method overridden in the subclass must be exactly the same as the parent method , It can be used @override Check whether it is an override method
Overload rule :
- The overloaded method must change the parameter list ( Number of parameters , Different type or order );
- Overloaded methods can change the return type ;
- Overloaded methods can change access modifiers ;
- Overloaded methods can declare new or broader check exceptions ;
- Methods can be overloaded in the same class or in a subclass .
- Cannot use return value type as distinguished standard of overloaded function .
Two 、 rewrite
Rewriting is a subclass's rewriting of the implementation process of the parent's accessible methods , Neither the return value nor the parameter can be changed . That is, the shell remains the same , Core rewrite !
The advantage of overriding is that subclasses can be as needed , Define your own actions . That is to say, the subclass can implement the methods of the parent class as required .
An overriding method cannot throw a new check exception or an exception that is broader than the overriding method declaration . for example : A method of the parent class declares a check exception IOException, But when overriding this method, it cannot be thrown Exception abnormal , because Exception yes IOException Parent class of , Throw out IOException Abnormal or IOException Subclass exception of .
In the object-oriented principle , Overriding means that you can override any existing method
3、 ... and 、 The difference between overloading and rewriting
| Difference point | Overloading methods | Rewriting methods |
|---|---|---|
| parameter list | Must be modified | It must not be modified |
| Return type | You can modify | It must not be modified |
Method rewrite (Overriding) And overloading (Overloading) yes java Different manifestations of polymorphism , Rewriting is a manifestation of polymorphism between parents and children , Overloading can be understood as a concrete manifestation of polymorphism .
- (1) Method overloading is defined in a class with more than one method with the same name , The number of parameters is different or the number is the same, but the type and order are different , It's called method overloading (Overloading).
- (2) Method rewriting is when a subclass has a method with the same name as the parent's method , And the number of parameters is the same as the type , The return value is the same way , It's called rewriting (Overriding).
- (3) Method overloading is a polymorphic representation of a class , Method rewriting is a kind of polymorphism between subclass and parent class .
边栏推荐
- Brief description of hystrix configuration
- 微服务feign调用时候,token丢失问题解决方案
- PyTorch
- IDEA settings设置快捷键实现字符串中的英文字母转大小写
- Leetcode 206. reverse chain list (2022.07.25)
- Solution to the problem of token loss when microservice feign is called
- Network ()
- "Door lock" ignites a heated discussion on the safety of living alone. The new poster picture is suffocating
- DADNN: Multi-Scene CTR Prediction via Domain-Aware Deep Neural Network
- 20220209 create a basic Servlet
猜你喜欢

Sort: merge sort and quick sort

How to close the high-level port

Network ()

Kdd2022 | uncover the mystery of Kwai short video recommendation re ranking, and recommend the new SOTA

PXE efficient batch network installation

Now developers are beginning to do testing. Will there be no software testers in the future?

基于Thinkphp的开源管理系统

动态性能视图概述

Unity metaverse (II), mixamo & animator hybrid tree and animation fusion

《门锁》引爆独居安全热议 全新海报画面令人窒息
随机推荐
1. MySQL Architecture [MySQL advanced]
Hcip--- MPLS detailed explanation and BGP route filtering
0 dynamic programming leetcode1567. Length of the longest subarray with positive product
Next item recommendations in short sessions
FTP service
1.MySQL架构篇【mysql高级】
Unity metaverse (II), mixamo & animator hybrid tree and animation fusion
Come across the sea to see you
Deep learning model deployment
NFS service and Samba service deployment
Audio and video learning (10) -- PS streaming
Common templates for web development
分布式相关面试题总结
A tutorial for mastering MySQL database audit characteristics, implementation scheme and audit plug-in deployment
Utils connection pool
ARIMA model for time series analysis and prediction
时间序列分析预测实战之ARIMA模型
TensorFlow学习日记之tflearn
Summarize the common high-frequency interview questions of the software testing post
[classic thesis of recommendation system (10)] Alibaba SDM model