Java rule on Methods with multiple parameters

Click For Summary

Discussion Overview

The discussion revolves around the behavior of Java methods and constructors that have multiple parameters, specifically focusing on what occurs when not all parameters are declared during a method call. The context includes introductory concepts related to method signatures, inheritance, and the implications for both methods and constructors.

Discussion Character

  • Conceptual clarification
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant inquires about the consequences of not declaring all parameters in a method call, using the DecimalFormat class as an example.
  • Another participant explains that Java matches method calls to method signatures, which include the number and types of arguments, and that a mismatch results in a compiler error.
  • A subsequent post seeks clarification on how inheritance affects the rules regarding method calls and parameters.
  • Another participant elaborates on the inheritance of methods from the NumberFormat class by the DecimalFormat class, detailing how the compiler selects the appropriate method based on the signature when a method is called with a specific argument.

Areas of Agreement / Disagreement

Participants generally agree on the importance of method signatures and the role of inheritance in determining method behavior, but the discussion remains open regarding the specific implications of these concepts in various scenarios.

Contextual Notes

There are unresolved aspects regarding the exact nature of errors that may arise when parameters are not declared, as well as the broader implications of inheritance on method resolution.

Deathfish
Messages
80
Reaction score
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
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.
 
Ok turns out has something to do with inheritance but how does this affect the rules?
 
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:

Similar threads

Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
15K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
2
Views
2K
Replies
3
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K