Java Learning (vii): Method overloading and method overrides, the This keyword, and the Super keyword

Source: Internet
Author: User
Tags throw exception

Method overloads and method overrides, the This keyword, and the Super keyword


1. Method overloading


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

Note:

The first is that the parameter list of the overloaded method must be different from the overloaded method, and that the difference must be sufficiently clear to determine which method to invoke;

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

For example, the most commonly used println () method defines more than 10 forms of overloading in the JDK's java.io.PrintStream, commonly used in the following format:

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

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

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

/********************************************************** * How to use method overloading and its 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, its 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 and create unique members in the subclass. However, if you create a method that is the same name, the same return value type, and the same argument list as the parent class, only the implementation in the method body differs from the functionality of the parent class, which is called a method override or method override.

Note:

The first is that the return value type and the parameter list must be the same as the returned value type and parameter list being overridden, otherwise it cannot be called rewriting;

The second is that the restriction of the access modifier must be greater than the access modifier of 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, can only throw IOException subclass exception, can throw non-check exception.

/********************************************************** * Method override usage and 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 corresponding constructor method of the parent class this. Detail=detail;}} Public String toString () {//rewrite Tostring//return "My name is:" +name+ ", The origin is:" +address+ ", characterized by:" +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. This keyword


The This keyword can be used to point to the current object within any instance method, or to the object on which the current method is called, or to use when the current type object reference is required.

Note: When the property name or member variable of a class is the same as the method parameter name that accesses the property, you need to use the This keyword to access the properties in the class, and the parameters in the properties and methods for the zone classification.


4. Super keyword


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

The super () method invokes the corresponding construction method of the parent class to complete the partial initialization of the subclass object.

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

The first is to call the constructor of the class's parent class through the Super statement in the class's constructor method.

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




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Learning (vii): Method overloading and method overrides, the This keyword, and the Super keyword

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.