These three concepts are related to polymorphism in Java. It's easier to distinguish between the two concepts of overloading and overriding, but hiding the concept makes the problem a little more complicated, and here's the difference. overloading means that different functions use the same function name, but the number or type of arguments for a function is different. When calling, different functions are distinguished according to the parameters of the function.
Overrides (also called overrides) are re-implemented in a derived class by re-implementing the virtual function in the base class (note that it is a virtual function). That is, the function name and parameters are the same, but the implementation of the functions of the body is not the same.
Hiding refers to a function in a derived class that masks a function of the same name in a base class. Hiding and the other two concepts appear to be very similar, it is difficult to distinguish, in fact, their key difference is the implementation of polymorphism. What does polymorphism mean? Simply to say is an interface, a variety of implementations. Overrides refer to the virtual function of a derived class that overrides a function with the same name and same parameters as the base class! Overload (Overload): Must be in a domain, the function name is the same but the function parameter is different, the role of overloading is the same function has different behavior, so not in a domain function is not overloaded, this is an important feature of overloading must be in a domain, and the inheritance is obviously in two classes Functions of the same function name, the relationship between the base class and the derived class can only be overridden or hidden. Hide: Refers to a member function of a derived class that hides a member function of a base class function. Hiding the word can be understood: when invoking a class's member function, the compiler will follow the class's inheritance chain to look up the definition of the function, if found then stop the search, So if a derived class and a base class have a function with the same name (regardless of whether the argument is the same), and the compiler eventually chooses a function in the derived class, then we say that the member function of the derived class "hides" the member function of the base class, meaning it prevents the compiler from continuing to look up the definition of the function.
Override (Override) 1, method name, parameter, return value are the same. 2. The subclass method cannot reduce the access rights of the parent class method. 3. The subclass method cannot throw more exceptions than the parent class method (but the subclass method can not throw an exception). 4, exists between the parent class and the child class. 5. The method is defined as final and cannot be overridden. Overload (Heavy) 1, parameter type, number, order at least one is not the same. 2. You cannot overload a method name that has only a different return value. 3, exists in the parent class and subclass, in the same class.
The rewriting of methods (overriding) and overloads (overloading) are different manifestations of Java polymorphism. Overrides (overriding) are a representation of the polymorphism between a parent class and a subclass, whereas overloading (overloading) is a representation of polymorphism in a class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding). When an object of a subclass uses this method, the definition in the subclass is called, and the definition in the parent class is "masked". If more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types or have different parameter order, which is called a method overload ( overloading). cannot be overloaded by Access permission, return type, thrown exception. 1. Override feature 1, the mark of the covered method must be matched with the mark of the method covered to achieve the effect of coverage; 2. The return value of the overridden method must be consistent with the return of the overridden method; 3. The exception thrown by the overridden method must match the exception thrown by the overridden method. or its subclass, 4. The overridden method cannot be private, otherwise only a new method is defined in its subclass, and it is not overwritten. 2.Overload features 1, the use of overloading can only be through a different parameter style. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must not be the same, for example, can be fun (int, float), but not fun (int, int)), 2, cannot pass access permission, return type, The exception that is thrown is overloaded; 3. The exception type and number of the method do not affect overloading; 4, for inheritance, if a method in the parent class is the access permission is PRIAVTE, then it can not be overloaded in the subclass, if defined, but also only defined a new method, but not to achieve the overloaded effect.
1, overload no relationship, override a bit of a relationship (the range of small can be a large range of override). 2. The overload return value type cannot be used as a different two function, and override should be exactly the same as the return type. ---------------------------------------------------------------
Overload:public A () {} public a (int a) {] has a method with the same name in a class, but the method has different parameters
Override:public class a{Public aaa () {]} public class B extends a {public aaa () {}} method AAA Override Also, overload typically occurs between methods of the same name with different parameter names and/or different number of parameters in the same class, but the return value of a method of the same name can be the same as override the more common is when subclasses inherit the parent class. Override some of the methods in the parent class to suit your needs. I seem to remember that the override method before the access control can not be lower than the control of the method in the parent class, that is, protected's parent method cannot be replaced by private or default, you can go to see the book, the book should be introduced.
---------------------------------------------------------------
>>overload (usually translated as overloaded or overloaded)
In general context, overload is the method (method), can refer to a class in the same name and the parameter type list is not the same way, the name represents the method is to be overload. Depending on the list of parameter types, the compiler determines which of the many methods that call the name.
>>override (usually translated as overlay)
In the general context, if the method is called override, that is, in the parent class of the class that defines the method, there is a method with the same name as the method and the parameter type list, in the subclass, the method is override. When the method is called on an instance of this subclass, the compiler knows exactly how to invoke the subclass.
Consider two of these classes:
Class Father {public void dosomething () {} public void dosomething (int a) {} Publi c void DoSomething (String s, int a) {}}
Class Son extends Father {public void dosomething (String s, int. a) {} public void dosomething (int A, String s) {}}
There are 3 dosomething methods in the Father class, the only difference being the list of parameter types, which are overload methods. Look at the subclass, the first method is also called DoSomething, and the parent class has a dosomething method and the method's parameter type list is the same, so this is override, and the second method, although the name is also called DoSomething, But its argument type list is inconsistent with the argument type list of other dosomething methods in the parent class and subclasses, so the compiler defaults this is a overload method. So how many ways do we actually invoke son's instance? As follows: New Son (). dosomething (); New Son (). dosomething (3); New Son (). DoSomething ("Hi", 5); New Son (). DoSomething (7, "Hello");
The first three are inherited from the parent class, and the third one is the public void dosomething (String s, int a) {} method of the called subclass, and the last is a newly defined overload method in the subclass.
Note: overload means that the same thing has multiple meanings in different places, and override means that it has only one meaning anywhere.
The difference between overloading and rewriting and the connection