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