This article uses examples to analyze the difference between overloading and rewriting in Java in detail. Interested friends can refer to it.
1. Overloading:
(1) Method overloading is a means for a class to handle different types of data in a unified way. Multiple functions with the same name exist at the same time, with different number/type of parameters.
Overloading is a manifestation of polymorphism in a class.
(2) Java method overloading means that multiple methods can be created in a class. They have the same name but different parameters and different definitions.
When calling methods, you decide which method to use based on the number and type of parameters passed to them. This is polymorphism.
(3) When overloading, the method names should be the same, but the parameter types and numbers are different, and the return value types can be the same or different. The return type cannot be used as a criterion for distinguishing overloaded functions.
Here is an example of overloading:
package c04.answer;//This is the package name//This is the first programming method of this program. First create a Dog class instance in the main method, and then use the this keyword in the constructor of the Dog class to call different bark method. Different overloaded methods bark are distinguished according to their parameter types. //Note: The compiler prohibits calling the constructor anywhere else except the constructor. package c04.answer;public class Dog {Dog(){this.bark();}void bark()//bark() method is an overloaded method {System.out.println(/"no barking!/"); this.bark(/"female/", 3.4);}void bark(String m,double l)//Note: The return values of the overloaded methods are the same, {System.out.println(/"a barking dog!/");this.bark(5, /"China/");}void bark(int a,String n)//Overloaded methods cannot be distinguished by return value, but can only be distinguished by "parameter type" and "class name" {System.out.println(/"a howling dog/"); }public static void main(String[] args){Dog dog = new Dog();//dog.bark(); [Page]//dog.bark(/"male/", /"yellow/");//dog.bark(5, / "China/");
2. Overriding
(1) Polymorphism between the parent class and the subclass, redefining the functions of the parent class. If a method defined in a subclass has the same name and parameters as its parent class, we say the method is overriding. In , subclasses can inherit methods from the parent class without having to rewrite the same methods.
But sometimes the subclass does not want to inherit the methods of the parent class unchanged, but wants to make certain modifications, which requires method rewriting.
Method overriding is also called method overwriting.
(2) If a method in the subclass has the same method name, return type, and parameter list as a method in the parent class, the new method will overwrite the original method.
If you need the original method in the parent class, you can use the super keyword, which refers to the parent class of the current class.
(3) The access modification permissions of subclass functions cannot be less than those of the parent class;
Here is an example of rewriting:
Concept: The mechanism for calling object methods.
The lowdown on dynamic binding:
1. The compiler checks the type and method name declared by the object to obtain all candidate methods. Try to comment out the test of the Base class in the above example, and then the compilation will not pass.
2. Overload decision: The compiler checks the parameter type of the method call and selects the only one from the above candidate methods (there will be implicit type conversion during this process).
If the compiler finds more than one or none, the compiler will report an error. Try to comment out the test(byte b) of the Base class in the above example, and the running result is 1 1.
3. If the method type is priavte static final and Java uses static compilation, the compiler will know exactly which method to call.
4. When the program runs and uses dynamic binding to call a method, the virtual machine must call the method version that matches the actual type of the object.
In the example, the actual type pointed to by b is TestOverriding, so b.test(0) calls the subclass's test.
However, the subclass does not override test(byte b), so b.test((byte)0) calls test(byte b) of the parent class.
If the (byte b) of the parent class is commented out, the implicit type is converted to int in the second step, and the test (int i) of the subclass is finally called.
3. Learning summary:
Polymorphism is a feature of object-oriented programming and has nothing to do with methods.
To put it simply, the same method can perform different processing according to different input data, that is, method overloading - with different parameter lists (static polymorphism)
When a subclass inherits the same method from the parent class and uses the same input data, but wants to respond differently from the parent class, you have to override the parent class method.
That is, rewrite this method in a subclass - same parameters, different implementations (dynamic polymorphism)
The three major characteristics of OOP: inheritance, polymorphism, and encapsulation.
public class Base{void test(int i){System.out.print(i);}void test(byte b){System.out.print(b);}} public class TestOverriding extends Base{void test(int i ){i++;System.out.println(i);}public static void main(String[]agrs){Base b=new TestOverriding();b.test(0)b.test((byte)0)}}
The output result at this time is 1 0, which is the result of dynamic binding at runtime.
The main advantage of overriding is the ability to define characteristics specific to a subclass:
publicclassFather{publicvoidspeak(){System.out.println(Father);}} publicclassSonextendsFather{publicvoidspeak(){System.out.println("son");}}
This is also called polymorphism. Overriding methods can only exist in inheritance relationships. Overriding methods can only override non-private methods of the parent class.
When the Father class speak() method is private in the above example, the Son class cannot override the Father class speak() method. At this time, the Son class speak() method is equivalent to a speak() method defined in the Son class.
Once the Father class speak() method is final, regardless of whether the method is modified by public, protected or default, the Son class cannot override the Father class speak() method at all.
When trying to compile the code, the compiler will throw an error. example:
publicclassFather{finalpublicvoidspeak(){System.out.println("Father");}}publicclassSonextendsFather{publicvoidspeak(){System.out.println("son");}}//The compiler will report an error;
When the Father class speak() method is modified by default, it can only be overridden by its subclasses in the same package. If it is not in the same package, it cannot be overridden.
When the speak() method of the Father class is prototyped, it is not only overridden by its subclasses in the same package, but can also be overridden by subclasses of different packages.
Rules for overriding methods:
1. The parameter list must be exactly the same as the overridden method, otherwise it cannot be called rewriting but overloading.
2. The return type must always be the same as the return type of the overridden method, otherwise it cannot be called overwriting but overloading.
3. The access modifier limit must be greater than the access modifier of the overridden method (public>protected>default>private)
4. The overridden method must not throw a new checked exception or a checked exception that is broader than the overridden method declaration. For example:
A method of the parent class declares a checked exception IOException. When overriding this method, you cannot throw Exception. You can only throw exceptions of subclasses of IOException, and you can throw unchecked exceptions.
And the overloaded rules:
1. Must have different parameter lists;
2. You can have a non-blaming return type, as long as the parameter list is different;
3. There can be different access modifiers;
4. Different exceptions can be thrown;
The difference between rewriting and overloading is:
Overriding polymorphism works. It can greatly reduce the amount of code input when calling overloaded methods. The same method name can have different functions or return values as long as different parameters are passed into it.
By making good use of rewriting and overloading, you can design a class with a clear and concise structure. It can be said that rewriting and overloading play an extraordinary role in the process of writing code.