Although these concepts are translated into Chinese, there are many different ways of translating them.
But in essence there are only two ways of saying that override and overload
Among them, overload are generally translated into overloaded
And the translation of override is a mess, so-called cover, overwrite, rewrite, rewrite refers to the fact that this means
So why are there so many nouns that cannot be unified?
To make the simplest analogy, why does the word "wife" have so many names? Instead of unifying it?
That's why I don't have to say it.
So, no matter how many words the word is translated into
In the course of your study, you just need to follow a statement you like to use it.
And for the rest of the story, you just need to see someone else use it.
To know that this is the same as the meaning of one of the statements you're using, it's okay.
So, the problem here can be simplified to
What is the definition and difference between overload and override?
For your convenience, here I will use the two nouns of "heavy" and "overwrite"
Where overload is overload, overwrite is override, you know it can be ~
Here's a definition of the most basic overloads and overrides:
In the same class , if more than one method name is defined, but can be distinguished by a parameter , then these methods can be called overloaded
In subclasses , this method in a subclass can be called a method that overrides a parent class if the method name, parameter, and return value are defined exactly as the parent class .
So, you just have to keep in mind the above two definitions, it's easy to distinguish between overloading and overwriting
The following is a combination of the above definitions, and then take a look at the code you gave:
[Code=java]
Class Superclass {
public void method (int x, double y) {
System.out.println ("I am a parent superclass");
}
public void method (int x) {
SYSTEM.OUT.PRINTLN ("I Must be overloaded");
}
}[/code]
As you can see here, because method (int x) and methods (int x, double y) have the same name, parameters can be distinguished
So these two method is the overloaded relationship
Then look at the following code:
[Code=java]
Class SubClass1 extends Superclass {
public void method (int x, double y) {
SYSTEM.OUT.PRINTLN ("I redefined the parent class superclass, which overrides the parent method");
}
}[/code]
As can be seen here, because SubClass1 inherited the superclass
and the Void method (int x, double y) defined in SubClass1
The method name, parameter, and return value of the void method (int x, double y) methods defined in superclass are identical
So this method in the subclass overrides the method in the parent class.
Well, if you can read the explanation of the above two-piece code,
Let me also give a piece of code, see if you can read it ~
[Code=java]
Class SubClass3 extends Superclass {
public void method (int x, double y) {
System.out.println ("I am a sub-class SUBCLASS3");
}
public void method (int x) {
System.out.println ("Am I overloaded or covered?") ");
}
}[/code]
In this piece of code, we can see
Since two method names are defined in SUBCLASS3, their relationship should be overloaded
And since two are defined exactly the same way in superclass, the relationship between them and the methods in the superclass is covered
So are they overloaded or covered? In fact, in the above two words, I have given the answer
in SubClass3 , the relationship of these two methods is overloaded
and Superclass , the relationship between the two methods is covered
So, you can think of them as overloading and as covering
The only difference is, from what angle do you look at them?
Well, there's so much to explain about overloading and covering.
For why to have these two concepts, and how to apply them, this can only rely on you later slowly to realize the hehe
Tips:
Let's add a little bit of detail here:
What can be distinguished by parameters? There are three main ways of distinguishing the following:
1. The number of parameters is different, such as method (int) and method (int, int)
2. Different types of parameters, such as method (int) and method (String)
3. The order of the parameters is different, such as method (int, String) and method (string, int)
* Note that the order of the parameters must be different if the order of several parameter types cannot be the same
For example, if it is a method (int, int), then no matter how the order is changed can not be differentiated
Excerpt from: A comment in the original article
Original source: http://bbs.itheima.com/thread-1379-1-1.html
Does the overload in Java have to be inside a class?