当前位置:网站首页>The difference between overloading and rewriting

The difference between overloading and rewriting

2022-07-06 13:39:00 Wake up duck, did you program today?

First , This question is often asked in interviews , But there is nothing between the two

heavy load :

Occurs in the same class , Method name must be the same , Different reference types , The number is different. , Different order , Method return value and decoration can be different , Occurs at compile time .

rewrite :

Occurs in a parent-child class , Method name , The parameter list can be different , The return value range is less than or equal to the parent class , A subclass cannot throw more exceptions than its parent class , The subclass access modifier range is greater than or equal to the parent class ; If the parent method access modifier is private, Then the subclass cannot override the method

The following situation is also wrong , Will report a mistake :

// The following compilation will report errors 
public int add(int a,int b)
public String add(int a,int b)

The above is my understanding of the difference between rewriting and overloading , If some friends have a better understanding , You can leave a message in the comment area !!!

原网站

版权声明
本文为[Wake up duck, did you program today?]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060916452861.html