Java method overrides and method overloading

Inheritance and polymorphism is object-oriented programming features. Using inheritance can then create a subclass of a parent class on the basis on this subclass only has existing properties and methods of the parent class, you can also create your own properties and methods. Because of the relationship between sub-classes and superclasses, which leads to a problem of method overloading and override methods. Method overloading and override the method in terms of inheritance and polymorphism applications will be many problems, it is easy to confuse these concepts, to grasp the difference between rewriting and overloaded write polymorphic way to learn to use the program, the program may increase maintenance basis.
First, the method of rewriting (0veriding)
how to define rewritten: In a Java program, the class hierarchy may produce a sub-class, subclass inherits the parent class, it has all the features of the parent class inherits all the methods of the parent class and variables. Subclasses can define new features, when the need to modify some of the subclasses of the parent class is extended, increasing the functionality, the designer often called a method of operating such a rewrite, also known as or referred to overwrite covered. Java rewrite reflects the favorable
if an instance of a subclass can inherit the parent class, then subclass have the right to override this method. Refers to: define a subclass of method, the type and methods consistent with the type of the parent class of this method is the type of the parent class or subtype method, and the name of this method, the number of parameters, parameter types, and parent class exactly the same way.
Objective: To subclass can override methods can hide inherited methods, it can subclass the parent class status and behavior change for their own state and behavior by overriding methods.
Restrictions: override the parent class method is not to reduce access methods.
Example:
class A {
void. AddClass (A int, int B, int C) {
int SUM = A + B + C;
IF (SUM>. 3) {
of System.out.print ( "SUM>. 3");
} the else {
System.out.print ( "sum <= 3" );
}
}
}
Class B {A the extends
void. AddClass (A int, int B, int C) {
int SUM = A + B + C;
IF (SUM>. 6) {
of System.out.print ( "SUM>. 6");
the else}
{
of System.out.print ( "SUM <=. 6");
}
}
}
public class C {
public static void main (String [] args) {
B B B = new new ();
b.addClass (1,1 , 1);
b.addClass (, 2, 3); // update judgment method, the main use
}
}
of rewriting is based on inheritance, language structure makes it richer. In Java, inheritance, the subclass can hide and methods of accessing both the parent class method inherited from the parent class can also be covered. Covering methods inherited from the parent class in Java is by overriding methods to achieve.
Rewriting method refers to a method called the child class have the exact same return type, method name, parameters and parameter types and number of the parent class inherited. Thus, the coverage of the parent class methods may be implemented. For example: The following code implements a method of rewriting.
class Person // defined parent class
fpublic void print () {// Method parent class
System.out.println (parent class Person print method!);
}
}
Class Student // define the extends Person Person class subclass inherits
{override public void print () {// method
System.out.println (subclass ! Print method of Student);
}
}
public class 0verrideExampleO1
{public static void main (String args [])
{S = Student new new Student ();
; S.print ()
}
}
run results: print subclass of Student!
as can be seen, when the child class overrides the parent class after print () method is invoked using the print S print subclass () method, the parent class () method is covered. That is, if now subclass method overrides the parent class, when calling certainly been rewritten method invocation, then if now some method to call the parent class how to do it? At this point, by using .. super key can achieve this function, super keywords from the content of the subclass can access the parent class, if you want to access methods are rewritten, the use of "super. method name (parameter list)" form to call .
For example:
Class Person
{public void print () {
System.out.println; (parent class Person print method!)
}
}
Class Student the extends the Person
{public void print () {
, super.print (: // access to the parent class through subclass override method
System.out.println ( "! Subclass Student's print method");
}
}
public OverrideExample02 class
{public static void main (String args [])
{S = Student new new Student ();
S.print ();
}
}
the result:! print methods of the parent class Person
! subclass Student's print method
if you want to use after the super keyword do not have to rewrite the method used
can also be clear that a method from the parent class is inherited. use super
but more specifically, from the parent class find, not in the child Find a class.
when subclasses need to modify some of the parent class to be extended, increased functionality, we often put such a method of operation is called rewrite.
subclass inherits the parent class or implement the interface. subclasses override the parent class or interface method must be a method name, return values, consistent with the parameter list. modifiers subclass override method must not higher than the parent method.
rewritten example
public class Fu {
public void fuMethod (String content) {
System.out.println ( "parent:" + content);
}
}
public class the extends Fu Zi { br /> @ Override
public void fuMethod (String Content) {
System.out.println ( "subclasses" + content);
}
}
Second, the rewrite rule
when rewriting method, you need to follow the following rules:
the same method parameter list (a) the list of parameters parent class method must be completely rewritten and quilt class, or can not call it rewriting It is overloaded. ..
(ii) the return type of the parent class and method must return the same type of overridden by subclasses, but otherwise can not rewrite called reload. ..
(c) Java stipulates that the method can not be overridden by subclasses have more stringent than the parent class method of access. Access size relationship:
write a Java program that people who know, the parent class is not in any case can be overridden when access to the parent class method modifier is private, the method can only by their own class to access can not be accessed outside the class, subclass can not be rewritten. If the parent class is defined as a method of public, the subclass definition as private, the error will be running. For example:
class Person
{public void print () (// public access
System.out.println ( "! Parent class Person print method");
}
}
Class Stedent the extends Person
{private void print () (// override method reduces access error
System.out.println ( "print method for a subclass Student!");
}
}
(d) due to the limitations of access to the parent class modifier It must be greater than the quilt class override methods of access modifier, while the smallest private rights, so if one access method in the parent class is private, so it can not be overridden in a subclass. If you redefine , but also defines a new approach will not achieve the effect of rewriting.
when (e) if the method throws an exception among the parent class, then override the parent class method in a subclass in the inheritance process, but also to throw an exception, but no more than exceptions thrown exception thrown parent class (parent class may be equal exception thrown). in other words, the overridden method must not throw new abnormalities, or ratio abnormally override method declaration broader examination type. For example, a parent class affirms a checked exception IOException, when you can not override this method throws exception, IOException subclass can only throw exceptions, you can Non-throw checked exceptions.
By the same token, if the child class creates a member variable, and the variable and
the same as a variable in the parent class name, known as variable or attribute rewrite coverage. But this
concept very few people go study it, because of little significance.
Third, the method overloading (overloading)
(a) how to define overloaded. overloaded method of rewriting and only a different word, many beginners think the two are very similar, it is not. methods heavy-duty class is to make a unified manner as a means of different types of data. they differ determined by the number and type of parameters passed to the method call which method the specific use, this is polymorphism.
A class can have many methods with the same name, but the parameters of these methods must be different, or a different number or type of parameters. The return type and parameter name of the method is not involved in the comparison, that is to say if the names of the two methods are the same, even though different types, but also must ensure that the different parameters.
Example:
class A {
int Computer (int A, int B) {
return A + B;
}
Double Computer (Double A, int B) {
return A * B;
}
}
public class E {
public static void main (String [] args) {
A = A new new A ();
of System.out.print (a.computer (10,26)); // output 36
of System.out.print (a.computer (10.0,26)); // output 260.0
}
}
called method overloading refers to a class, a plurality of the same method name of the method, but a different parameter list. It refers to a parameter list different number of parameters, parameter types, or in different order parameter. Overloaded methods will be frequently used in practical applications. Not only is the general method, constructor can also be overloaded. To analyze the following by an example.
Definition and use overloaded.
{The Person class
{String name;
int Age;
void Print () {
} } In the above procedure, there can be seen a plurality of Person class named void print method, which is overloaded methods. The implementation of the program, the results are as follows:






















Name: Li Ming Age: 22
Name: Wang Early Age: l9
Name: Jinbo Age: No 18ID: 10 00325
Name: Wan Ning Age: 25ID number: 110903
when overloaded method, requires the presence of some connection between the methods, because it can improve the readability of the program, generally overloaded functions similar method overloading rules. Overload means that we can define some of the same name of the method, different parameter definitions to distinguish between these methods, then the time, Java virtual machine will call the root
executed according to a list of different parameters to select the appropriate method. That is, when an overloaded method is invoked, Java .. and with the type of the parameter (or) number of overloads to determine the actual call www.fx61.com/brokerlist/xm.html XM rebate. Thus, the number of parameters for each type or overloaded method must be different. Although each overloaded methods may have different return types,
but not enough to distinguish the type of return used which method. When Java calls an overloaded method is invoked method parameters and parameter matching is performed. In the heavy-duty use should pay attention to the following points:
1. When using overloaded only through a different parameter list, you must have different parameter list. For example, different types of parameters, number of different parameters, different parameters order. Of course, several types of parameters within the same method must be different, for example, may be fun (int, float), but not as fun (int, int).
2. can not access, return type, exceptions thrown overloaded.
3. exception type and number of methods will not affect overloaded. ..
4. The return can have different types, different parameter list as long as it.
5. You can have different access modifiers.
6. You can throw different exceptions.
A: In the same class, the same method name, but not the same as the method parameter list, called overloaded methods.
Different parameter list, the following three conditions:
a, number of parameters, like
B, is not the same type of parameter
C, the order parameter is not the same
example of method overloading: comparing whether the design data is equal to two, the whole integral type compatible byte, Short, int, Long
public class OverLoad {
public static void main (String [] args) {
int S1 = 10;
int S2 = 10;
System.out.println (Compare (S1, S2));
}
public static Boolean Compare (S1 byte, byte S2) {
the System .out.println ( "byte ...");
return == S1 S2;
}
public static Boolean Compare (Short S1, S2 Short) {
System.out.println ( "Short ...");
return == S1 S2;
}
public static Boolean Compare (int S1, S2 int) {
System.out.println ( "int ...");
return == S1 S2;
}
public static Boolean Compare (S1 Long, Long S2) {
System.out.println ( "Long ...");
return == S1 S2;
}
}
based on the parameters passed types, find the corresponding method.
Determines whether overload: Look method name, a list of parameters (number, type, order). Like the method name, parameter list is not the same.
Fourth, the method of rewriting and overloaded methods distinguished
by analyzing the above example, we can rewrite the methods and the difference between the total overloaded
form such a form as follows:
point of difference
overloaded
rewriting (overwriting)
the English
Overloading is the
overiding
defined
the same method name, the number of parameters of different types or
method name, parameter types, the return values are all the same type
of permission is not required
method has not been rewritten more restrictive permission
range
occurs in a class
occurs in the derived class
method overload: in the same class, the name of the same methods, but a different number of parameters, parameter types, or sequentially;
method overloading Example:
public class Student {
public void the name () {}
public void the name (int a) { }
public void the Name (int A, B String) {}
public void the Name (String B, int A) {}
}
Error Method overloading
public class Student {
public void the Name () {}
public String the Name () {return "hello"}
}
method overload regardless of the type of the return value, the code being given above;
Method rewritten: In a subclass inherits when the parent, the parent class and method subclass same name exists, then the method subclasses would override the parent class methods;
override Note:
method name and parameter list 1. the method of the child and parent class to consistent
2. subclass access modifier must be greater than or equal access to the parent class modifier
exception subclass 3. exception thrown is less than the parent method throws the
method of Example override
public class father {
public void showAge () {
System.out.println ( "father");
}
}
public class the extends father son {
public void showAge () {
System.out.println ( "son")
}
public static void main (String [] args) {
Son Son = Son new new ();
son.showAge ();
}
}
The code above operating results are: son, if showAge method of removing the Son, the results are: father; if you want to rewrite the method in the parent class called again, you can use the super keyword;
in object-oriented programming thoughts , inheritance and polymorphism class is mainly reflected in the parent class subclass rewrite. The reload constructor overloads as a typical example in a method can be expressed by a variety of initialization behavior of the object constructor overloads. Flexible use of method overloading and override the method can not only reduce the workload of encoding, can greatly improve the maintainability and scalability of the program. Good use of rewriting and overloading
can design a clear and concise structure of class, it can be said rewritten and overloading in the process of writing code in an unusual role.

Guess you like

Origin blog.51cto.com/14511863/2440570