Today is the third week to go to work in the company. Due to my own reasons, I still cannot write my blog every day, so my blog has been missing for two weeks, today, I finally knocked on my keyboard to write a blog.
I have been reviewing the basic knowledge of Java for the past two weeks. I am waiting for the notice from China Southern Airlines to pass the examination. I can't help but find some questions at the company every day, review. I am a little sleepy in the morning. I really hope that China Southern Airlines will inform me of the past assessment soon. If I can pass the examination, I will find a new job if I cannot! It's no big deal! But my requirements for myself are: Pass is required! I am confident in myself. According to the recent review, I should not have too many questions about Java basics. However, if I take an examination of any software engineering introduction or C ++, I will be confused. To tell the truth, the scope is really large and our project team is hard to grasp! I would like to ask if many of my friends have obtained Java-related questions from China Southern Airlines? Can I make some contributions? If you can, send it to my mailbox! Address: 1035776548@qq.com
Thank you!
Oh! Sorry, I am a little confused. I just mentioned method rewriting and method overloading! Today, I am reviewing method rewriting and method overloading. I still don't know much about it. After searching for information, I have probably understood a lot! I would like to share with you the following.
First, method rewriting and method overloading are based on Java's Object-oriented Inheritance and polymorphism features. I will not talk about Object-oriented Inheritance and polymorphism. Inheritance refers to the creation of a subclass based on a parent class, so that the subclass has non-private features of the parent class, And the subclass can also expand its own features, this leads to method rewriting and method overloading!
I. Override)
How to define rewriting in Java: the inheritance feature of classes in Java programs can generate a subclass, And the subclass inherits the parent class with non-private attributes (methods and variables) of the parent class ), you can add attributes (methods and variables) in the subclass, and expand methods in the parent class to enhance your functions. This is called rewriting, it is also called "Rewrite" or "Overwrite. The so-called method rewriting means that the subclass method and the inherited method in the parent class have the same method name, return value type, number of parameters of the method, and parameter type, so that it can be called method rewriting.
Code embodiment:
// This is the definition of the parent class
Public class person {
Public void eat (){
System. Out. println ("===== this is the Eat method of the parent class person ====== ");
}
}
// This is the definition of the subclass.
Public class student extends person {
@ Override
Public void eat (){
System. Out. println ("=== this is the Eat method of the sub-class student === ");
}
// Test the main method.
Public static void main (string [] ARGs ){
Student = new student ();
Student. Eat (); // output: === this is the class student's eat method ===
}
}
After a subclass overrides the method of the parent class, it calls the method in the subclass when instantiating the subclass. The method of the parent class is overwritten. If you need to call the method of the parent class in the subclass, use the super keyword in the subclass method to call the method of the parent class. Format: Method Name (parameter list) in the super. parent class ).
Rewrite Rules:
To override a method, you must follow the following rules:
(1) The parameter list of the subclass method must be the same as that of the override method in the parent class (number of parameters and parameter type). Otherwise, the method can only be overloaded.
(2) the return value type of the subclass method must be the same as the return value type of the method overridden in the parent class. Otherwise, only method overloading can be implemented.
(3) According to Java, the access permission of subclass methods cannot be smaller than that of Methods rewritten in the parent class, and must be greater than or equal to the access permission of the parent class.
(4) During the rewriting process, if an exception is thrown for the method to be overwritten in the parent class, the method in the subclass also throws an exception. However, the thrown exception also has certain constraints.> A subclass cannot throw more exceptions than the parent class. It can only throw exceptions smaller than the parent class, or does not throw an exception. For example, if the parent class method throws an exception, The subclass can only throw an ioexception or throw an exception that is smaller than the exception or does not throw an exception.
Ii. Method overload)
How to define the overload in Java: write multiple methods in the class in Java. The method names of these methods are the same, the list of parameters of a method is different (the number of parameters and the type of parameters are different. The so-called method overload is: in a class, there are a series of methods with the same method name, but the parameter list is different, this type of method is now method overload. Note that the constructor can also be overloaded.
Code embodiment:
// This is the definition of the parent class
Public class person {
/*
* Protected void eat () throws exception {
* System. Out. println ("===== this is the Eat method of the parent class person ====== ");}
*/
Public Person (){
System. Out. println ("this is a construction method without Parameters ");
}
Public Person (string name ){
System. Out. println ("this is a constructor parameter name =" + name );
}
Void print (){
System. Out. println ("=== print method without parameters === ");
}
Void print (string Str ){
System. Out. println ("==== there is a print method of the parameter ==== ");
}
Void print (string STR, int ){
System. Out. println ("=== print method with two parameters === ");
}
}
When an object calls these methods, Java VM executes different method bodies based on different parameter lists, which is a manifestation of polymorphism.
Reload rules:
The following rules must be followed for method overloading:
(1) When using method overloading, you must use different parameter lists in the method to overload the method. For example, the number of parameters of a method is different or the type of parameters of a method is different.
(2) You cannot use access permissions, return value types, and thrown exceptions to overload
(3) The method exception type and the number of thrown exceptions do not affect method overloading. That is to say, different exceptions can be thrown in overloaded methods.
(4) There can be different return value types, as long as the parameter list of the method is different.
(5) There can be different access Modifiers
III,Differences between rewriting and overloading
Differences |
Heavy Load |
Rewrite (overwrite) |
English |
Overloading |
Overiding |
Definition |
The method name is the same, and the type or number of parameters are different. |
The method name, parameter type, and return value type are all the same |
No permission required |
The method to be rewritten cannot have more strict permissions. |
Range |
Occurs in a class |
Occurs in the inheritance class |
Note: constructor methods cannot be inherited, so they cannot be overwritten. In subclass, the constructor of the parent class can only be called using the super keyword, but can be overloaded.
Connection: http://blog.csdn.net/zhangyabinsky/article/details/7047330
It mainly describes the differences between method rewriting and method overloading, so you can learn from Java beginners!