Crazy Java Learning Notes Object-oriented (vi)-constructor overloading, method overloading, and method rewriting

Source: Internet
Author: User

One, method overloading (overload):

Java allows multiple methods of the same name to be defined in the same class , as long as the parameters are not the same, if the same class contains two or more methods with the same name , but the parameter list is different , it becomes the method overload ( two identical). .

At the same time here we need to pay attention to the following two points :

A. Method overloading has no relation to the return value type ;

B. The method overload has no relation to whether there is a static adornment .

So how do we determine which method to call ?

A. Keynote speaker: who is it that calls this method, is the class or object

B. Method name and formal parameter: Determines which method is called ( as determined by the method name and formal parameters )

Example 1:

1  Public classtestmethodload{2      Public voidTest () {3System.out.println ("~ ~ No formal parameter test method ~ ~");4     }5      Public voidTest (String name) {6System.out.println ("~ ~ with a formal parameter of the test method ~ ~, the parameter name is:" +name);7     }8 9      Public Static voidMain (string[] args) {TenTestmethodload T =Newtestmethodload (); One t.test (); AT.test ("Hello"); -     } -}

Example 2:

1  Public classtestmethodload2{2      Public voidTest (String name) {3System.out.println ("method with one parameter, parameter is:" +name);4     }5      Public voidTest (String ... msgs) {6          for(String msg:msgs) {7System.out.println ("A method with variable string type arguments, the parameter is:" +msg);8         }9     }Ten  One      Public Static voidMain (string[] args) { ATestMethodLoad2 TL =NewTestMethodLoad2 (); -Tl.test ("Hello"); -Tl.test ("123", "JKL"); the  -     } -}

Second, the constructor reloads:

Constructors can think of it as a special method: There is no need to declare a return value type , and if the return value type is declared, it becomes the normal method;

  the role of the constructor : the constructor is not an object creation, but an initialization instance, strictly speaking, the object is new ;

  constructor Overloading : exactly like method overloads --Two identical (same class, method name)--one difference (the parameter list is different)

1  Public classmonkey{2     PrivateString name;3     Private Doubleweight;4     Private intAge ;5 6     //provides a constructor with two parameters7      PublicMonkey (String name,Doubleweight) {8          This. Name =name;9          This. Weight =weight;Ten     } One  A     //A constructor with three parameters is provided, and the constructor is used to initialize the instance's -      PublicMonkey (String name,DoubleWeight,intAge ) { -          This(name,weight); the          This. Age =Age ; -     } -  -      Public voidinfo () { +System.out.println ("This is a monkey, its name is:" + name + -", Weight is:" + weight + ", age is:" +Age ); +     } A  at      Public Static voidMain (string[] args) { -Monkey m =NewMonkey ("Little King Kong", 100); - m.info (); -Monkey m2 =NewMonkey ("King Kong", 300,20); - m2.info (); -     } in}

Iii. method Override (Override)/method override:

  A method override is used between a subclass and a parent class, and a method overload is used for the same class, and the subclass can override the parent class's methods when the subclass inherits from the parent class and does not really meet the subclass requirements.

  1. Method Rewrite rules:

  two same: method name is same, formal parameter list is same;

  two small : the return value type of the subclass override method must be smaller or equal than the return value type of the parent class method, and the declaration of the subclass override method must throw an exception that is less or equal than the declaration of the parent class method

  a large : Subclass overriding methods must have access permissions greater or equal than the parent class .

  2. @Override: forcing subclasses to override method identifiers

Function: An error occurs when the compiler is not compiled, prevents an error in overriding the parent class method , and allows the compiler to perform a stricter check, requiring that the method to be decorated must be a method of overriding the parent class

1  Public class bird{2      Public void Fly () {3         System.out.println ("This is a bird in the blanks to fly ~ ~ ~"); 4     }5 }
1  Public classtuoniao{2      Public voidFly () {3System.out.println ("The ostrich is so stupid that he can only run on the ground!"));4     }5      Public Static voidMain (string[] args) {6Tuoniao tn =NewTuoniao ();7Tn.fly ();//because the subclass overrides the Fly method, the final call is a subclass-overridden method8     }9}

Attention:

1  Public classGoldmonkeyextendsmonkey{2     //subclasses will get all the field and method from the parent class, without overriding the parent class constructor, the default is to inherit the empty constructor of the parent class, and if the parent class does not have an empty constructor, the error3      PublicGoldmonkey (String name,DoubleWeight,intAge ) {4          This. Name =name;5          This. Weight =weight;6          This. Age =Age ;7     }8   9      Public Static voidMain (string[] args) {TenGoldmonkey GM =NewGoldmonkey ("Kim", 300,12); One         //invoke the method inherited from the parent class A gm.info ();//Kawai class does not override the Info method, and by default inherits the info method of the parent class -     } -  the     //subclasses can override the parent class's methods when they inherit from the parent class and do not really meet the subclass requirements -     //protected void info () {}//the error subclass override method must be greater or equal than the access permission of the parent class.  -  -      Public voidinfo () { +         //extends is inherited from all methods and properties of the parent class, so it has properties such as name, weight, age, and so on -         //However, because the parent class access is private so you cannot access it, you need to modify the parent Class field property to protected/default +System.out.println ("This is a golden monkey, the name is:" + name + ", weight is:" + weight + ", age is:" +Age ); A     }   at}

Crazy Java Learning Notes Object-oriented (vi)-constructor overloading, method overloading, and method rewriting

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.