In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about the difference between override and overload in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Java::override vs overload
Java grammar is so delicate that it attracts countless heroes to bend over.
(1) override:
Class Parent {
Object func (Number n) {
...
}
}
Class Child extends Parent {
Object func (Number n) {
...
}
}
This is so-called override.
The meaning of override is commonly known as "polymorphism".
Child.func and Parent.func can be seen as occupying the same Entry in the virtual function pointer table. That is, Child.func covers Parent.func.
Parent vtable
Entry 1:Parent.func
Child vtable
Entry 1:Child func
Override is the program run-time dynamic decision which method to call, so it is called: polymorphism.
(2) overload
Class Child {
Object func (Number n) {
...
}
Object func (String s) {
...
}
}
This is so-called overload.
Note: two func methods have the same name, but the parameters are different, so they are completely different methods. These two func methods occupy two different Entry in the virtual function pointer table.
Vtable
Void func (Number)
Void func (String)
Attention:
When overload compiles, it decides which method to call. The compiler generates JVM instructions with different calling function addresses (different Entry) according to different parameter types.
How to translate BTW:overload into overload? I really don't understand. Overload means overload in English. I strongly doubt that the word "overload" is a coined word, like Alpine White. It can only be understood, but it cannot be put into words.
(3)
Class Parent {
Object func (Number n) {
...
}
}
Class Child extends Parent {
Object func (String s) {
...
}
}
This is also overload.
Parent vtable
Entry 1:void func (Number n) of parent
Child vtable
Entry 1:void func (Number n) of parent
Entry 2:void func (String s) of child
(4)
Class Parent {
Object func (Number n) {
...
}
}
Class Child extends Parent {
Object func (Number n) {
...
}
Object func (String s) {
...
}
}
This example code has both override and overload.
Parent vtable
Entry 1:void func (Number n) of parent
Child vtable
Entry 1:void func (Number n) of child
Entry 2:void func (String s) of child
= > Conclusion:
A:Strongly advise to avoid using "overload".
B:Strongly advise to use different method names.
For instance:
Objcet funcNumber (Number n) {
...
}
Object funcString (String s) {
...
}
End.
Thank you for reading! This is the end of the article on "what is the difference between override and overload in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.