当前位置:网站首页>Overrides vs overloads of methods
Overrides vs overloads of methods
2022-06-22 04:32:00 【Frank___ seven】
Method rewrite (override)
- Method rewrite
Subclasses inherit methods from their parent classes , Sometimes , The subclass needs to modify the implementation of the method defined in the parent class , This is called Method rewrite (method overriding).“ rewrite ” The concept of “ heavy load ” be similar , They are Java“ polymorphic ” One of the techniques , So-called “ heavy load ”, That is, the method name is the same , But you can do different things on different occasions . When a subclass inherits a parent , And the names of the methods in the subclass and the methods in the superclass 、 When the number and type of parameters are identical , This method in the subclass is said to override the method in the superclass .“ rewrite ” Also known as “ make carbon copies ”、“ Cover ”. - How to use rewrite
classSuper{ Access right Method return value type Method 1( Parameters 1) {...}}classSubextendsSuper{ Access right Method return value type Method 1( Parameters 1) —————> Duplicate methods in the parent class {...}}
Be careful : Method rewriting must follow two principles , Otherwise, the compiler will point out the program error .
- Overridden methods cannot have more strict access than overridden methods ;
- Overridden methods cannot generate more exceptions than overridden methods ( About anomalies , I'll introduce it later ).
Method overloading (overload)
- Method overloading
First, let's review the method overloading mentioned earlier , Method overloading Multiple methods can have the same name , however The number or type of parameters cannot be exactly the same . When calling a method , The compiler determines the method currently used according to the number and type of parameters . Method overloading brings convenience to programming , yes OOP The concrete realization of polymorphism . stay Java In the class library of the system , Overloading many important methods , It provides convenience for users to use these methods . - Overloaded rules
- The overloaded method must change the parameter list ( The number or type of parameters is different );
- 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 .

边栏推荐
- Storage structure of tree
- New chief maintenance personnel for QT project
- Odoo 开发手册(一)接触 odoo 的第二天...
- Solid smart contract interview questions
- DFS of graph
- 音频帧大小的计算
- Fonctionnement de base du tableau de séquence
- [you don't understand the routing strategy? Try it!]
- TCL Huaxing released the world's first 0.016hz ultra-low frequency OLED wearable device screen
- Accumulate ERP global parameters, flexibly configure cost value rules, and complete accounting
猜你喜欢

After the active RM machine is powered off, RM ha switching is normal. However, the cluster resources cannot be viewed on the yarnui, and the application is always in the accepted state.

The best time to climb a ladder & sell shares (notes of the runner)

SQL operation: with expression and its application

Odoo Development Manual (I) the second day of contact with odoo

Use putty to configure port mapping to realize the access of the external network to the server
![[BP regression prediction] optimize BP regression prediction based on MATLAB GA (including comparison before optimization) [including Matlab source code 1901]](/img/73/1e4c605991189acc674d85618cf0ef.png)
[BP regression prediction] optimize BP regression prediction based on MATLAB GA (including comparison before optimization) [including Matlab source code 1901]

Active learning overview, strategy and uncertainty measurement

套用这套模板,玩转周报、月报、年报更省事

希尔排序

Low power radar sensing module, application of smart lock radar sensing scheme, smart radar sensor technology
随机推荐
High precision positioner formwork
Odoo 开发手册(一)接触 odoo 的第二天...
PCM data format
Tencent side
[从零开始学习FPGA编程-39]:进阶篇 - 语法-硬件模块的单元测试:仿真激励、testbench
[shell] method of adding 1 to 100
[day 23] given an array of length N, return the array after deleting the x-th element | array deletion operation
[BP regression prediction] optimize BP regression prediction based on MATLAB GA (including comparison before optimization) [including Matlab source code 1901]
Take you to develop an efficiency enhancing tool -- vscode plug-in
Existing requirements synchronize other database user information to our system. Their primary key ID is string and our primary key is long
POSIX shared memory
With these websites, do you still worry about job hopping without raising your salary?
Solid smart contract interview questions
Cloud security daily 220621: Intel microcode vulnerability found in Ubuntu operating system, which needs to be upgraded as soon as possible
冒泡排序
Use putty to configure port mapping to realize the access of the external network to the server
Sequential implementation of queues
Use of markdown markup language
Design and implementation of ks004 based on SSH address book system
Interaction between C language and Lua (practice 3)