Why is format method from NumberFormat class calling subclass format method?

Sam

I have a Main.java and in the main method I have the following code:

totalSalaries = 14000;
System.out.printf("The total payout should be %s%n", currencyInstance.format(totalSalaries));

I tried to run the above statement in debug mode to understand the traversal of how the format method gets called.

When you step-in the above format method, it inturn calls the below format method which belongs to NumberFormat class (where NumberFormat extends Format)

public final String format(long number) {
    return format(number, new StringBuffer(),
                  DontCareFieldPosition.INSTANCE).toString();
}

Upon then, clicking on the format method above (of NumberFormat class) within this return block, it seems to land on the below which is part of the DecimalFormat class (where DecimalFormat extends NumberFormat).

@Override
public StringBuffer format(long number, StringBuffer result,
                           FieldPosition fieldPosition) {
    fieldPosition.setBeginIndex(0);
    fieldPosition.setEndIndex(0);

    return format(number, result, fieldPosition.getFieldDelegate());
}

Could someone kindly explain why the NumberFormat class is calling format method of it's subclass DecimalFormat? It shouldn't be possible isn't it ? Thanks for your help

Thomas Kläger

The first thing to observe is that NumberFormat is an abstract class:

public abstract class NumberFormat

That means that when you call NumberFormat.getCurrencyInstance(); what you will get is an instance of a concrete subclass of NumberFormat - in most cases that will probably be a DecimalFormat instance.

The format(long number) method in NumberFormat is

public final String format(long number) {
    return format(number, new StringBuffer(),
                  DontCareFieldPosition.INSTANCE).toString();
}

And it calls the method format(long number, StringBuffer toAppendTo, FieldPosition pos) which in NumberFormat is declared as abstract method:

public abstract StringBuffer format(long number,
                                    StringBuffer toAppendTo,
                                    FieldPosition pos);

For a class to be instanciable it must not have any abstract methods - if it extends an abstract base class, it must implement all the abstract methods of its parent class. And if you look at the implementation of DecimalFormat it implements that method:

public StringBuffer format(long number, StringBuffer result,
                           FieldPosition fieldPosition) {
    fieldPosition.setBeginIndex(0);
    fieldPosition.setEndIndex(0);

    return format(number, result, fieldPosition.getFieldDelegate());
}

What you see in the debugger (that it steps through the format method in NumberFormat and then calls the format method in DecimalFormat) is due to the fact the method format(long number) is declared in the NumberFormat class. The currencyInstance is however always a DecimalFormat instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

format method in NumberFormat Java

From Java

Why is "this" from superclass calling method from subclass?

From Dev

Calling class method inside string format

From Java

Calling a subclass method from superclass

From Java

Java: calling a super class' protected method from a subclass - not visible?

From Dev

Why is my value null when calling superclass method from subclass?

From Dev

Passing a calling class into a subclass and calling a method from it when the subclass has completed a job

From Dev

Java: calling subclass implementation of abstract base class method from base class itself but with subclass instance

From Dev

stackoverflow while calling method of parent class in subclass

From Dev

Calling a method from a class

From Dev

Why does calling a method from base class calls the child method?

From Dev

javascript why calling a method from another method in the same class need this?

From Dev

Why is my superclass calling my subclass method?

From Dev

Java Inheritance: Why is it not calling the method of the subclass?

From Dev

Calling a Subclass method from an array of Superclass Objects

From Dev

Calling constructor from a subclass method with a generic parameter

From Dev

calling an abstract method from the subclass object

From Dev

c++ calling method from subclass in superclass

From Dev

Calling and passing to a method from class

From Javascript

Calling a class method from the constructor

From Dev

Calling method from State class

From Dev

Calling a function from a class method

From Dev

Calling method from a different class

From Dev

calling a method from tkinter class

From Java

Calling method of Another class from run() method

From Javascript

Calling a method from another method in the same class

From Dev

Calling an instance method from a class method - Ruby

From Dev

Ruby: why doesn't calling super.method work from subclass

From Java

why overridden method calling from Subclass if i have done up-casting?

Related Related

  1. 1

    format method in NumberFormat Java

  2. 2

    Why is "this" from superclass calling method from subclass?

  3. 3

    Calling class method inside string format

  4. 4

    Calling a subclass method from superclass

  5. 5

    Java: calling a super class' protected method from a subclass - not visible?

  6. 6

    Why is my value null when calling superclass method from subclass?

  7. 7

    Passing a calling class into a subclass and calling a method from it when the subclass has completed a job

  8. 8

    Java: calling subclass implementation of abstract base class method from base class itself but with subclass instance

  9. 9

    stackoverflow while calling method of parent class in subclass

  10. 10

    Calling a method from a class

  11. 11

    Why does calling a method from base class calls the child method?

  12. 12

    javascript why calling a method from another method in the same class need this?

  13. 13

    Why is my superclass calling my subclass method?

  14. 14

    Java Inheritance: Why is it not calling the method of the subclass?

  15. 15

    Calling a Subclass method from an array of Superclass Objects

  16. 16

    Calling constructor from a subclass method with a generic parameter

  17. 17

    calling an abstract method from the subclass object

  18. 18

    c++ calling method from subclass in superclass

  19. 19

    Calling and passing to a method from class

  20. 20

    Calling a class method from the constructor

  21. 21

    Calling method from State class

  22. 22

    Calling a function from a class method

  23. 23

    Calling method from a different class

  24. 24

    calling a method from tkinter class

  25. 25

    Calling method of Another class from run() method

  26. 26

    Calling a method from another method in the same class

  27. 27

    Calling an instance method from a class method - Ruby

  28. 28

    Ruby: why doesn't calling super.method work from subclass

  29. 29

    why overridden method calling from Subclass if i have done up-casting?

HotTag

Archive