Java notes-method overloading and method rewriting

Source: Internet
Author: User

Read Catalogue
    • Method overloading
    • Method overrides
First, method overloading

1) in the same class, the method overload is used if you want to create multiple methods with the same name. Method overloads are distinguished by the parameters of the same name, the parameters can be of different types, the number is different, or the order is different

Package Com.example;public class T{public void Show () {System.out.println ("show ()"); /*error public int Show () {return,} */public void Show (int i) {System.out.println ("show" + "(" + i + ")");} public void Show (String str) {System.out.println ("show" + "(" + str + ")"); public void Show (int i, String str) {System.out.println ("show" + "(" + i + "," + str + ")"); public void Show (String str, int i) {System.out.println ("show" + "(" + str + "," + i + ")");}
Package Com.example;public class Test{public static void Main (string[] args) {T t = new T (); T.show (); t.show; T.show ("Zhang San "); T.show (10," Zhang San "); T.show (" Zhang San ", 10);}}
Run Result: Show () show (Zhang San) show (10, Zhang San) show (Zhang San, 10)
    • Method names cannot be differentiated by changing the type of the return value, but by changing the type of the parameter, the number of arguments, or the order of the parameters.

2) The ability to create multiple constructors in a class also takes advantage of method overloading

Package Com.example;public class T{public T () {}public T (int i) {}public T (String str) {}}

3) When the method overloads, if the argument list is a basic data type, the compiler automatically matches the incoming parameter to the appropriate type. If there are no matching types, the compiler will either type or narrow the arguments

Example 1:

Package Com.example;public class T{public void F1 (char i) {System.out.print ("F1 (char)");} public void F1 (byte i) {System.out.print ("F1 (Byte)"); public void F1 (short i) {System.out.print ("F1 (short)");} public void F1 (int i) {System.out.print ("F1 (int)");} public void F1 (long i) {System.out.print ("F1 (Long)");} public void F1 (float i) {System.out.print ("F1 (float)");} public void F1 (double i) {System.out.print ("F1 (Double)");} public void F2 (byte i) {System.out.print ("F2 (Byte)"); public void F2 (short i) {System.out.print ("F2 (short)");} public void F2 (int i) {System.out.print ("F2 (int)");} public void F2 (long i) {System.out.print ("F2 (Long)");} public void F2 (float i) {System.out.print ("F2 (float)");} public void F2 (double i) {System.out.print ("F2 (Double)");} public void F3 (short i) {System.out.print ("F3 (short)");} public void F3 (int i) {System.out.print ("F3 (int)");} public void F3 (Long i) {System.out.print ("F3 (Long)");} public void F3 (float i) {System.out.print ("F3 (float)");} public void F3 (dOuble i) {System.out.print ("F3 (Double)");} public void f4 (int i) {System.out.print ("F4 (int)");} public void F4 (long i) {System.out.print ("F4 (Long)");} public void F4 (float i) {System.out.print ("F4 (float)");} public void F4 (double i) {System.out.print ("F4 (Double)");} public void F5 (long i) {System.out.print ("F5 (Long)");} public void F5 (float i) {System.out.print ("F5 (float)");} public void F5 (double i) {System.out.print ("F5 (Double)");} public void F6 (float i) {System.out.print ("F6 (float)");} public void F6 (double i) {System.out.print ("F6 (Double)");} public void F7 (double i) {System.out.print ("F7 (double)");} public void Showchar () {char i = 0; System.out.println ("char:"); F1 (i); F2 (i); F3 (i); F4 (i); F5 (i); F6 (i); F7 (i); System.out.println ("\ n");} public void Showbyte () {byte i = 0; System.out.println ("byte:"); F1 (i); F2 (i); F3 (i); F4 (i); F5 (i); F6 (i); F7 (i); System.out.println ("\ n");} public void Showshort () {Short i = 0; System.out.println ("short:"); F1 (i); F2 (i); F3 (i); F4 (i); F5 (i); F6 (i); F7 (i); System.out.println ("\ n");} public void Showint () {int i = 0; SYSTEM.OUT.PRINTLN ("int:"); F1 (i); F2 (i); F3 (i); F4 (i); F5 (i); F6 (i); F7 (i); System.out.println ("\ n");} public void Showlong () {Long i = 0; System.out.println ("Long:"); F1 (i); F2 (i); F3 (i); F4 (i); F5 (i); F6 (i); F7 (i); System.out.println ("\ n");} public void Showfloat () {Float i = 0; System.out.println ("float:"); F1 (i); F2 (i); F3 (i); F4 (i); F5 (i); F6 (i); F7 (i); System.out.println ("\ n");} public void showdouble () {Double i = 0; System.out.println ("double:"); F1 (i); F2 (i); F3 (i); F4 (i); F5 (i); F6 (i); F7 (i); System.out.println ("\ n");}}
Package Com.example;public class Test{public static void Main (string[] args) {T t = new T (); T.showchar (); T.showbyte (); t.sh Owshort (); T.showint (); T.showlong (); T.showfloat (); t.showdouble ();}}
Run Result: char:f1 (char) f2 (int) f3 (int) f4 (int) f5 (long) f6 (float) F7 (double) byte:f1 (byte) f2 (Byte) F3 (short) f4 (int) F5 (long ) f6 (float) F7 (double) short:f1 (short) F2 (short) F3 (short) f4 (int) f5 (long) f6 (float) F7 (double) int:f1 (int) f2 (int) F3 ( INT.) f4 (int) f5 (long) f6 (float) F7 (double) long:f1 (long) F2 (Long) F3 (long) F4 (long) F5 (long) f6 (float) F7 (double) float:f1 (float) F2 (float) F3 (float) F4 (float) f5 (float) f6 (float) F7 (double) double:f1 (double) F2 (Double) F3 (double) F4 (double) F5 (Double) f6 (double) F7 (double)
    • The char type is particularly special, when there is no char type in the overloaded method, the passed argument is promoted to the int type instead of the byte type, for example: The F2 method and the F3 method, both of which do not overload the char type parameter, so the passed-in parameter is promoted directly to the int type, from the running knot The first line of the fruit can see this feature

Example 2:

Package Com.example;public class T{public void F1 (char i) {System.out.print ("F1 (char)");} public void F1 (byte i) {System.out.print ("F1 (Byte)"); public void F1 (short i) {System.out.print ("F1 (short)");}}
Package Com.example;public class Test{public static void Main (string[] args) {int i = 0; T t = new t ();//t.f1 (i); ERRORT.F1 ((char) i); T.f1 ((byte) i); T.f1 ((short) i);}}
Run Result: F1 (char) F1 (byte) F1 (short)
    • If the incoming parameter requires narrowing transformation, you must explicitly force the type conversion on the passed-in parameter

[Back to top]


Second, Method rewrite

1) method overrides exist only in subclasses (inheriting or implementing interfaces)

Package Com.example;public class person{//Parent public void speak () {System.out.println ("person:speak ()");}}
Package Com.example;public class Man extends person{//subclass public void Speak () {//Override speak method for parent class Super.speak ();//Call SP of parent class Eak method System.out.println ("Man:speak ()");}}
Operation Result: Person:speak () Man:speak ()
    • method is overridden, the method name, return value type, and parameter list of the subclass method should be the same as the method in the parent class

2) The access control permission of a method overridden in a subclass cannot be lower than the access control permission for a method in the parent class

Package Com.example;public class person{//parent class public void P1 () {}protected void P2 () {}void p3 () {}private void P4 () {}}
Package Com.example;public class Man extends person{//subclass public void P1 () {//the Access control permission for the P1 method of the parent class is public, then the access control permission for the P1 method of the subclass can only be public}/* public void P2 () {///can be public or protected} */protected void P2 () {}/* public void P3 () {}protected void P3 ( ) {} */void p3 () {}/* public void P4 () {}protected void P4 () {}void P4 () {} */private void P4 () {}}

3) In an inheritance relationship, subclasses can also perform method overloads on methods of the parent class

Package Com.example;public class person{//Parent public void speak (int i) {System.out.println ("Person:speak" + "(" + i + ")") ;}}
Package Com.example;public class Man extends person{//subclass public void Speak (String str) {//method overload System.out.println ("Man:s Peak "+" ("+ str +") ");}}
Package Com.example;public class Test{public static void Main (string[] args) {mans m = new Man (); M.speak ("Zhang San");}}
Operation Result: Man:speak (Zhang San)

4) Java in order to ensure that the overriding method without error, so introduced the @Override

Package Com.example;public class person{//Parent public void speak (int i) {System.out.println ("Person:speak" + "(" + i + ")") ;}}
Package Com.example;public class Man extends person{//subclass/* @Overridepublic void Speak (String str) {System.out.println ("M An:speak "+" ("+ str +") ");} *///error, with @Override, then @Override the following method must be a method override, or compile with an error @overridepublic void speak (int i) {System.out.println ("Man: Speak "+" ("+ i +") ");}}
Package Com.example;public class Test{public static void Main (string[] args) {mans m = new Man (); M.speak (10);}}
Running Result: Man:speak (10)

[Back to top]


Resources:

The 4th edition of Java programming ideas


end~

Java notes-method overloading and method rewriting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.