- A overload
- Two Override
- Tri-Polymorphism
I. Overload
Many methods in the same class can have the same name, as long as their parameter declarations are different, and this method is called overloading.
As an example:
Public classoverload{"'' A heavy output method '"' voidPrint () {System. out. println ("hello! "); }"'' overload the above output method, plus the parameter '"' voidPrint (String name) {System. out. println (name +", hello! "); }"'' overload the above output method, plus two parameters '"' voidPrint (String name,intHeight) {System. out. println (name +", hello! You have "+ Height +"cm high?" "); } Public Static void Main(String args[]) {Overload ol =NewOverload ();"'' Create an object ol '"'Ol.print (); Ol.print ("Make-Boy"); Ol.print ("Make-Boy", the); }}
Operation Result:
Hello!
Hello!, the boy.
Hello!, the boy. Do you have 180cm height?
The above example defines 3 methods with the same name, but there are different parameter types and parameter types, the use of overloading is actually to avoid a variety of method names, some of the functions are similar, if you re-establish a method, re-fetch the method name, which makes the program section is not easy to read. It's quite similar to the sqlhelper you used to make the system.
The essence of overloading:
- Method name is the same
- The number of parameters can be different
- Parameter types can be different
When accessing an overloaded method, the compiler will first compare the parameter type with the actual calling method using the worthwhile type, has chosen the correct method, and if there is no matching method, the compiler error.
Two. Override
When it comes to coverage, you have to talk about one of the three main characteristics of object-oriented: inheritance; In the process of inheriting, subclasses can directly use the method inherited by the parent class, but when the subclass has a method that declares the same name, the method called is the subclass, which is called overwrite.
See an example:
Public class father{ "'' Create method print () '"'voidPrint() {System.out.println ("This is the parent class feature!" "); } Public Staticvoid Main (String args[]) {Father F =NewFather (); Son s =NewSon (); F.Print(); S.Print(); }} class son extends father{ "'' Create sub-class son inherit father '"'voidPrint(){"'' Overwrite method print () '"'System.out.println ("This is a subclass feature!" "); } }
Operation Result:
This is the attribute of the parent class!
This is a sub-class feature!
As can be seen from the above code, subclasses redefine the print () method of the class parent class, and the subclass overrides the same method as the parent class, and the knowledge method body is different.
Three. Polymorphism
Polymorphism is a variety of forms, in Java, polymorphic mainly refers to have the same form, but different parameters to achieve different functions.
First talk about overloading:
‘‘‘创建一个学生类‘‘‘publicclass student{ student() ‘‘‘无参数构造函数‘‘‘ {} student(string name) ‘‘‘带一个参数的构造函数‘‘‘ {} student (string name,string code) ‘‘‘带两个参数的构造函数‘‘‘}
The above three examples are the overloaded functions of 3 constructors, with the same form, but they differ according to the parameters, the implementation of the function is also different, this is polymorphic, so that overloading is actually polymorphic, or that overloading is a form of polymorphism.
And then again covering, why is it also polymorphic? Because the method of the parent class is overridden in a subclass, polymorphism is a variety of forms, the subclass and the parent class have the same method name, but the completion function is not the same, so that overrides are polymorphic.
It can be said that overloading and covering are two forms of polymorphism.
Beginner Java (vii)----Override VS overload