Java method signature

Source: Internet
Author: User

The name and argument list of the method becomes the signature of the method. Note that the method signature does not include the return type of the method. This article tests to understand the true meaning of the parameter list and how the return value of the method should be related to the return value of the method in the parent class when overriding the method in the inheritance.

Here are three helper classes:

Package Methodsign;public class Ancestor {}

Package Methodsign;public class Parent extends Ancestor {}

Package Methodsign;public class Other {}

The following is a test class, and the test results are described in the comments:

Package methodsign;/** * Test Method signature * Method name and parameter list is called method signature * @author Yuncong * */public class Person {private String name;private String gender;private int age;public person () {//TODO auto-generated Constructor stub}/** * by constructor 1 and constructor 2 cannot exist simultaneously, the parameter list of the method signature This refers to the list of types of parameters; * by both Constructor 1 and constructor 3, the list of parameter types for the method signature is also related to the order of the parameter types, that is, * even if the number of arguments in the method brackets is exactly the same as the type, as long as the order of the parameter types is different, the parameter type list */// Constructor 1public Person (string name, String gender, int.) {super (); this.name = Name;this.gender = Gender;this.age = age;} Constructor 2public Person (string Gender, string name, Int. age) {super (); this.name = Name;this.gender = Gender;this.age = age;} Constructor 3public Person (string name, int age, string gender) {super (); this.name = Name;this.age = Age;this.gender = gender;} Public Ancestor GetAncestor () {return new Ancestor ();}}

Package Methodsign;public class Student extends Person {/** * Method 1 is correct, method 2 is wrong, the method that overrides the parent class in the subclass, the return value of the method * Must be the same as the return value of the method in the parent class or a subclass of the method return value in the parent class *///method 1public parent GetAncestor () {return new parent (); Method 2public Other GetAncestor () {Return to new Other ();}}


Java method signature

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.