当前位置:网站首页>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 .
边栏推荐
- IDEA settings设置快捷键实现字符串中的英文字母转大小写
- Kdd2022 | uncover the mystery of Kwai short video recommendation re ranking, and recommend the new SOTA
- Now developers are beginning to do testing. Will there be no software testers in the future?
- Add traceid to the project log
- A tutorial for mastering MySQL database audit characteristics, implementation scheme and audit plug-in deployment
- No valid host was found when setting up openstack to create an instance There are not enough hosts available. code:500
- Installation of Baidu flying paste deep learning framework tutorial in Anaconda
- 2022.7.22DAY612
- Matlab drawing black spots on two / three-dimensional drawings
- 数据库基础
猜你喜欢

Regular expression rules and common regular expressions

Regression analysis code implementation

DADNN: Multi-Scene CTR Prediction via Domain-Aware Deep Neural Network

【uniapp】多种支付方式封装

Lnmp+wordpress to quickly build a personal website

Fang Wenshan, Jay Chou's best partner, will officially announce "Hualiu yuancosmos" on July 25

VScode无法启动问题解决思路

20220209 create a basic Servlet

Enterprise private network construction and operation and maintenance

深度学习模型部署
随机推荐
Hcip--- BGP comprehensive experiment
Network trimming: a data driven neuron pruning approach towards efficient deep architectures paper translation / Notes
机器学习相关比赛网站
OVS底层实现原理
DADNN: Multi-Scene CTR Prediction via Domain-Aware Deep Neural Network
Shardingsphere data slicing
Lambda and stream
Idea shortcut key
Database foundation
2022.7.22DAY612
Matlab-二/三维图上绘制黑点
Brief description of hystrix configuration
JMeter性能测试之使用CSV文件参数化
分布式相关面试题总结
音视频学习(十)——ps流
OVSDB
Anaconda 中安装 百度飞浆Paddle 深度学习框架 教程
Solution to the problem of token loss when microservice feign is called
Hcip--- MPLS detailed explanation and BGP route filtering
C# 使用Log4Net记录日志(基础篇)