Reputation: 1882
According to javadoc
An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.
My question is - is the order of parameters immaterial? In case that thay have same name and they are the same type as well?
Upvotes: 5
Views: 5091
Reputation: 39
Note that when you are working with multiple parameters, the method call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.
Source :https://www.w3schools.com/java/java_methods_param.asp
Upvotes: 0
Reputation: 120586
The Java Language Specification says the order is material, but it requires some teasing out to explain why:
8.4.1. Formal Parameters
The formal parameters of a method or constructor, if any, are specified by a list of comma-separated parameter specifiers.
...
8.4.2. Method Signature
Two method or constructor declarations M and N have the same argument types if all of the following conditions hold:
- They have the same number of formal parameters (possibly zero)
- They have the same number of type parameters (possibly zero)
- Let A1, ..., An be the type parameters of M and let B1, ..., Bn be the type parameters of N. After renaming each occurrence of a Bi in N's type to Ai, the bounds of corresponding type variables are the same, and the formal parameter types of M and N are the same.
...
Two method signatures m1 and m2 are override-equivalent iff either m1 is a subsignature of m2 or m2 is a subsignature of m1.
The formal parameters are specified as a list, so for "the formal parameter types of M and N" to be the same, they must be the same list and lists are order-dependant.
Since the correspondance in 3 is order dependent, the order of type parameters also matters.
This becomes more obvious when you deal with the byte-code/JNI convention for Method Descriptors.
MethodDescriptor: ( ParameterDescriptor* ) ReturnDescriptor
Upvotes: 2
Reputation: 46239
The order matters, so two methods with the same arguments in different order are not considered to have the same signature.
For example, this example does not compile:
interface Foo {
void doIt(String what, int times);
}
class Bar implements Foo {
public void doIt(int times, String what) {}
}
The names of the parameters however, are irrelevant. This is perfectly fine:
interface Foo {
void doIt(String what, int times);
}
class Bar implements Foo {
public void doIt(String andNowForSomeThingCompetelyDifferent, int theLarch) {}
}
Upvotes: 6
Reputation: 65
If the order is not the same the compiler will not be able to find the method, and you will get an error saying as such
Upvotes: 0
Reputation: 855
no, the order matters!
Rules for Overriding Methods in Java
Rule 1) A method is said to be overriden if a class which extends another class defines method with the same name and arguments list.
Rule 2) The method defined in the base class should be visible in the derived class. If this is not so, the method in the derived class will not be considered overridden version but will be treated as a normal method.
Rule 3) The method name and arguments list should be same for overriding and overridden methods. But the return type can be co-variant. This means that if the return type of the method in super class is Map, then the return type of the same method can be HashMap.
Rule 4) The access specifier in the overriding method (in the derived class) should not be more limiting than that of the overriden method (in the base class). This means that if the access specifier for base class method is protected then the access specifier for the derived class method should not be default or private but can be protected, public. The order of increasing visibility of various specifiers is:
private, default, protected, public
Rule 5) The exceptions specified in the derived class method should be either same or sub-class of them. Thus if the base class method specifies the exception as IOException in the throws clause then the derived class method can specify the exception as FileNotFoundException, IOException but not Exception.
Upvotes: 1
Reputation: 1108
The order matters. The signature is different if the order is different.
public void foo(int x, Object y)
cannot be overridden with
public void foo(Object y, int x)
Upvotes: 0