Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java rule on Methods with multiple parameters

  1. Oct 2, 2013 #1
    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: Oct 2, 2013
  2. jcsd
  3. Oct 2, 2013 #2

    jedishrfu

    Staff: Mentor

    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.
     
  4. Oct 2, 2013 #3
    Ok turns out has something to do with inheritance but how does this affect the rules?
     
  5. Oct 2, 2013 #4

    Mark44

    Staff: Mentor

    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 (Text):
    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: May 6, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java rule on Methods with multiple parameters
Loading...