Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does java distinguish between overloading and rewriting

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Java is how to distinguish between overloading and rewriting. For this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more small partners who want to solve this problem find a simpler and easier way.

This problem is relatively simple, suitable for small white, mainly distinguish between overload and rewriting of methods. Sometimes confused, the following is a good way to sort out the distinction.

In a class, there can be many constructor methods, and the same name is the class name, in fact, these methods have constituted overloading. Rewriting means that within a class there can be many different methods with the same name. Now the question is, how does java distinguish between these methods?

method overload

In fact, there is a very simple way to distinguish overloading, that is, each method has a unique parameter list.

public class Son extends Father{

int fff(){

return 0;

}

String fff(int x,String y){

return "";

}

void fff(String y,int x){};

public static void main(String[] args) {

Son son=new Son();

}

}

From the above code you can see:

1. Different number of parameters can be distinguished

2, the order of parameters can be distinguished.

Note: Different return value types cannot be distinguished.

Why can't we distinguish between different return value types? This is because sometimes we define a method that we simply want to call, not get its return value.

For example, when fff() is called, you only need to call fff() or fff(1,"data") without getting its return value. Jvm is indistinguishable.

Method rewriting starts with the concept:

In a subclass, methods inherited from the base class can be overridden as needed.

The overridden method and the overridden method must have the same method name, parameter list, and return type. (Note: Return values can be subtypes of return values in parent classes.) If the parameter is a subclass, it is not overridden.

Overwriting methods cannot use stricter access rights than the method being overridden.

An overriding method cannot declare that it throws exception types larger than the scope of the overridden method.

Summary: rewriting is a change of the same method inherited by the parent class by the child class. This change needs to follow the format according to the format of the parent class. Access permissions, throwing exceptions, etc. are all within the control of the parent class method. Internal specific implementations can achieve different effects.

In layman's terms: rewriting is rewriting the same method of the parent class, but how do people know that you are rewriting, not adding a new method? This requires that the format must be the same as the parent class. When others look at it, ah, they look exactly the same (referring to the rules 2, 3, and 4 above), they must have been rewritten, that is, rewritten.

Consider an example:

Define parent class:

public class Father extends Grandpa {

String add(int a,int b){

int c=a+b;

return ""+c;

}

}

Definition of Subclasses

public class Son extends Father{

String add(int a,int b){

int c=a+b;

return ""+c;

}

public static void main(String[] args) {

Son son=new Son();

}

}

This completes the rewriting of the method.

About java is how to distinguish between overloaded and rewritten questions to share the answer here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 221

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report