Record some notes on learning Java.
Overloading#
Overloading refers to method overloading, where the characteristic of overloading is that two or more methods have the same name.
Classic Overloading Methods#
Overloading is mostly used for constructor methods, because the role of constructors is to initialize objects, and the name of the constructor must be the same as the class name, which forms a classic pair of overloaded methods: the parameterless constructor and the parameterized constructor.
Here is an example:
Why Overloading is Needed#
We all know that the most important concept in Java is: objects. Methods are the behaviors of objects. An object can perform the same named behavior but do different things, for example: Xiaoming playing mahjong.
Literal meaning: Xiaoming is playing mahjong with others.
Other meanings: Xiaoming is "hitting" a person named "mahjong".
Therefore, the ambiguity in natural language can also occur in code.
For example, when a person is eating, whatever I give him, he is eating it. If I give him noodles, his behavior is eating noodles. If I give him rice, he is eating rice. But his behavior is called eating.
In summary, overloading is necessary in code.
Distinguishing Overloaded Methods#
If different methods have the same name, how can we distinguish them?
The important principle for distinguishing overloading is: each overloaded method must have a unique parameter type list (i.e., each method has different parameters from other methods with the same name).
Where are the differences in the parameter list?
Differences in Parameter Types#
Output:
Differences in Parameter Order#
Although distinguishing overloaded methods by parameter order is possible, it is not recommended to use this method to distinguish, because it is difficult to maintain the code in this way.
Output:
Distinguishing Overloaded Methods by Return Value#
In addition to the types and order of parameters, can we distinguish overloaded methods by the return value of the method?
The answer is no, here is an example.
When we only want to call the f()
method, but do not need its return value (such as the System.out.println
method), if we only call the f()
method, Java cannot distinguish which method we need to call.
Overloading of Primitive Types#
For overloaded methods with only different primitive types, there may be a situation where the smaller type can be automatically promoted to a larger type.
Output:
The variable x
is of type int
, but the checkBasicTypes()
method does not have an int
parameter. When the program is running, it will find a method with a parameter type larger than int
to call, that is, the data type passed in is smaller than the parameter type of the method, and the data type passed in will be automatically promoted. (int -> long)
Overriding#
Overriding generally occurs between parent and child classes, where the parent class and the child class have two methods with the same name and parameter list. Because they have the same method name, when the method is called, the child class method will override the same-named parent class method.
Here is an example of overriding:
Output:
In the above example, both the parent class and the child class define the say()
method, which is actually called overriding the say()
method of the parent class.
After overriding the method, when we call the say()
method of the child class object, even though the type of the Dog object is Animal, Java will still call the say()
method of Dog, because the method of the child class will override the same-named method of the parent class.
Principles of Overriding#
If you need to override a method, you need to meet the Liskov Substitution Principle:
- The access permission of the subclass method must be greater than or equal to the parent class method.
- For example, if the access modifier of the parent class method is protected, the access modifier of the subclass method can only be protected or public.
- The return type of the subclass method must be the same as the return type of the parent class method or its subtype.
- For example, B extends A: the return type of the parent method is A, then the return type of the subclass method can be A or B.
- The exception type thrown by the subclass method must be the same as the exception type thrown by the parent class method or its subtype.
@Override Annotation#
@Override
annotation is not a keyword in Java, but it can be used like a keyword. If this annotation is added to the overriding method, the compiler will help you check whether it meets the three restrictions mentioned above, and check whether the overriding method is legal. Sometimes it can also effectively prevent accidental overloading.