Java rule on Methods with multiple parameters

In summary: In this case, the method that the compiler finds is the nonabstract format method, which takes a double and returns "0.00".
  • #1
Deathfish
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:
Technology news on Phys.org
  • #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 ), 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:
  • #5


The Java rule on methods with multiple parameters states that all parameters must be declared in order for the method to be properly executed. In the example given, the method DecimalFormat has three parameters: number, toAppendTo, and pos. If you do not declare all three parameters when using the method, an error will occur. This is applicable to both methods and constructors.

In the provided example, if you only declare the number parameter when using the DecimalFormat method, an error will occur because the other two parameters (toAppendTo and pos) have not been declared. In order for the method to work correctly, all three parameters must be declared.

This rule is important because it ensures that all necessary information is provided for the method to perform its intended function. If parameters are left undeclared, the method may not be able to properly execute and could potentially return incorrect or unexpected results.

In terms of inheritance, this rule applies to child classes inheriting methods from parent classes. If a child class inherits a method with multiple parameters from a parent class, the child class must also declare all of the parameters in order for the method to work correctly. If the child class does not declare all of the parameters, an error will occur.

In summary, it is important to always declare all parameters when using a method with multiple parameters in order to ensure proper execution and avoid errors. This rule applies to both methods and constructors and is also relevant in the context of inheritance.
 

1. What is the purpose of having multiple parameters in a Java method?

Having multiple parameters allows a method to accept and process different types of data simultaneously, making it more versatile and efficient. It also allows for more specific and complex calculations or operations to be performed within the method.

2. How do you declare and use multiple parameters in a Java method?

To declare multiple parameters in a method, you simply list them within the parentheses after the method name. For example:
public void calculateSum(int num1, int num2) {
//method body
}
To use these parameters within the method, you can refer to them by their respective names, such as num1 and num2 in this example.

3. Can a method have a variable number of parameters in Java?

Yes, using the "varargs" syntax, a method can have a variable number of parameters in Java. This means that the method can accept an indefinite number of arguments of the same type. For example:
public void printNumbers(int... numbers) {
//method body
}
This method can be called with any number of integers as arguments.

4. How do you handle multiple parameters with different data types in a Java method?

You can handle multiple parameters with different data types by specifying the data type for each parameter when declaring the method. Java will automatically convert the data types to match the parameter types when the method is called. Additionally, you can use type casting to convert between different data types within the method if needed.

5. What are some best practices when using methods with multiple parameters in Java?

Some best practices for using methods with multiple parameters in Java include keeping the number of parameters to a minimum to avoid confusion and make the method more reusable, using descriptive and meaningful parameter names, and considering the order in which the parameters are listed to make the method more intuitive and easy to use.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
749
  • Programming and Computer Science
Replies
5
Views
15K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top