overriding : Overrides are subclasses of methods that override the parent class, with two important features
1. Subclass method Name and parameter type, number must be the same as the parent class.
2. If there is a return value, the return value type must also be the same as the parent class.
1 //Parent Class2 Public classBird () {3 PublicString GetInfo () {4 return"GetInfo method in parent class";5 }6 }7 8 //subclasses overriding the GetInfo method of the parent class9 Ten Public classLittlebirdextendsbird{ One @Override A PublicString GetInfo () { - return"GetInfo Methods in subclasses"; - } the}
overloading : Overloading is to allow a method to have multiple implementations in a class, with different parameter types and numbers.
When overloaded. The method name must be the same, the number of parameter types and the return value are not required.
1 Public classplus{2 Public intPlusintAintb) { 3 returnA +b;}4 Public intPlusintAintBintc) {//Plus method overloads5 returna+b+C;}6 Public DoublePlusDoubleADoubleb) {//Plus method overloads7 returnA +b;}8 9 Public Static voidMain (String args[]) {TenPlus Twoint =NewPlus (); OnePlus Threeint =NewPlus (); APlus twodouble =NewPlus (); -SYSTEM.OUT.PRINTLN ("Call is the Plus Method 1, the result is:" +twoint.plus (5,6));//the plus method is called by default when the parameter is two int values 1 -SYSTEM.OUT.PRINTLN ("Call is the Plus Method 2, the result is:" +twoint.plus (5,6,8));//the plus method is called by default when the parameter is three int values 2 theSYSTEM.OUT.PRINTLN ("Call is the Plus Method 1, the result is:" +twoint.plus (5.42,6.62));//the default call to the Plus Method 3 when the parameter is a two double value - } -}
The difference between overrides and overloads is that:
Overriding polymorphism works, and calling overloaded methods can greatly reduce the amount of code input, and the same method name can have different functions or return values as long as it passes different parameters inside.
With good rewriting and overloading, you can design a clear and concise class that can be said to be overridden and overloaded in a way that is very unusual in the process of writing code.
Differences between Java overrides and overloads