Java Learning (vii): Method overloading and method rewriting, Thiskeyword, and Superkeyword

Source: Internet
Author: User

Method overloads and method overrides, Thiskeyword, and Superkeyword


1. Method overloading


Overloading allows classes with the same name but different number and type parameters to be passed to the method.

Note:

First, the list of overloaded methods must be different from the overloaded method, and the difference must be sufficient to clearly determine which method to invoke;

The second is that the return value type of the overloaded method can be different from the overloaded method, but only the different return value types cannot be represented as overloads.

For example, the most frequently used println () method defines more than 10 forms of overloading in the JDK's java.io.PrintStream, often using formats such as the following:

public void println (int i) {...}

public void println (double i) {...}

public void println (String i) {...}

/********************************************************** * The use of "method overloading" and its demo sample code */public class Overloadrewrite{void num (int i) {System.out.println ("received is an int type parameter, its value =" +i);} void num (float j) {System.out.println ("Received is a float type parameter.) Its value = "+j");} void num (string k) {System.out.println ("Received is a String-type parameter whose value =" +k);} public static void Main (String [] args) {overloadrewrite a=new overloadrewrite (); A.num (+); A.num (100.20f); A.num (" Hello! ");}}


2. Method rewriting


When a subclass inherits from a parent class, it can have member methods and member variables in the parent class, creating a unique member in the subclass. However, suppose you create a method with the same name in the parent class, the same return value type, and the same list of parameters, only the implementation in the method body is different, to implement a function different from the parent class, which is called a method override or method override.

Note:

The first is that the return value type and the list of parameters must be the same as the returned value type and the list of parameters that are overridden. Otherwise, it cannot be called rewriting.

The second is that the restriction of the access modifier must be greater than the access modifier for the overridden method (Public > Protected > Default > Private);

Third, the overriding method must not throw a new check exception or a more general type of check exception than the overridden method declaration.

For example, a method of the parent class declares a check exception ioexception, when overriding this method, cannot throw exception, only can throw the IOException subclass exception. Ability to throw non-check exceptions.


/********************************************************** * "method override" Usage and demo sample code */public class overloadrewrite{ public string Name;public string Address;public overloadrewrite (String name,string Address) {//constructor method this. Name=name;this. address=address;} Public String toString () {//rewrite Tostringreturn "My name is:" +name+ ", The origin is:" +address;} public class Sichuancai extends overloadrewrite{//inherits public string Detail;public Sichuancai (string name,string address, String detail) {//constructor method super (name, address);//super calls the constructor method corresponding to the parent class this. Detail=detail;}} Public String toString () {//rewrite Tostring//return "My name is:" +name+ ". The origin is: "+address+", the characteristic is: "+this." Detail;//}public static void Main (String [] args) {overloadrewrite food=new overloadrewrite ("noodles", "Italy"); SYSTEM.OUT.PRINTLN (food), Sichuancai sichuan=food.new sichuancai ("spicy", "Chengdu", "Spicy and spicy"); System.out.println (Sichuan);}}


3, Thiskeyword


The Thiskeyword can be used to point to the current object in any instance method, or to an object on which the current method is called, or when the current type object reference is required.

Note: When a class's property name or member variable is the same as the method parameter that visits the property, you need to use Thiskeyword to access the properties in the class. The parameters in the properties and methods that are categorized by the zone.


4, Superkeyword


Because the subclass cannot inherit the constructor of the parent class, to invoke the constructor of the parent class, you must use Super () in the first row of the constructor method of the child class.

The super () method invokes the parent class's corresponding construction method, completing the partial initialization of the subclass object.

Note: The program is required to use super () keyword in the following two situations:

The first is in the constructor method of the class. The construction method of the parent class of the class is called through the Super statement.

The second is to access the members of the parent class in the subclass.




Java Learning (vii): Method overloading and method rewriting, Thiskeyword, and Superkeyword

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.