Java rule on Methods with multiple parameters

In this case, the method that the compiler finds is the nonabstract format method, which takes a double and returns "0.00".f
  • #1
86
0
Let's say a method or constructor has multiple parameters example in DecimalFormat --

public final StringBuffer format(double number,
StringBuffer toAppendTo,
FieldPosition pos)

if you don't declare all of the parameters, what happens to those that are not declared?

for example

DecimalFormat dec = new DecimalFormat("0.##");
String convert = dec.format(1.3588); //double number parameter is declared

here the parameters StringBuffer toAppendTo, FieldPosition pos are not declared. what happens to these?

Is it applicable to both methods and constructors? Any cases where it will return an error?
(do keep it simple because I am just doing an intro course)

Edit : It has something to do with inheritance, someone explain in simple terms thanks!
 
Last edited:
  • #2
Java looks at how you call the method and tries to find a method that has the same signature.

By signature I mean same number of arguments, same type or subtype of arguments. By subtype I mean the method calls for an Animal argument and you provide a Dog object (which extends the Animal class).

If not then you'll get a compiler error.

Constructors work the same way.
 
  • #3
Ok turns out has something to do with inheritance but how does this affect the rules?
 
  • #4
The DecimalFormat class inherits from the NumberFormat class, which means that in addition to the methods and properties that are defined in the DecimalFormat class, a DecimalFormat instance also has access to the public methods and properties that are defined in the NumberFormat class.

Looking at the docs for the NumberFormat class (http://docs.oracle.com/javase/1.4.2/docs/api/java/text/NumberFormat.html [Broken]), I see that there are five format methods, two of which are abstract. Two of the nonabstract methods take a single parameter (of type double and long, respectively) and return a String.

With regard to an abstract method in a base class, a class that inherits from the base class cannot use the abstract method in the base class - it has to provide its own implementation of that method. For example, the method whose prototype you show in post 1 implements (i.e., provides a definition for) the abstract format method in the NumberFormat class. Both the abstract method in the NumberFormat class and the nonabstract method in the DecimalFormat class have the same signatures (same number and types of arguments, and same return type).

When you call format like this -
Code:
String convert = dec.format(1.3588);
the compiler looks for all of the format methods in the DecimalFormat class and any classes it inherits from (all the way back to Object, which all classes inherit from), and chooses the method that has one argument of type double and that returns a string.
 
Last edited by a moderator:

Suggested for: Java rule on Methods with multiple parameters

Back
Top