Can a method return an abstract class type?

前端 未结 1 1014
傲寒
傲寒 2021-01-06 16:57

I am looking the method getCurrencyInstance() in NumberFormat class and I see that this method has a return type of NumberFormat.

相关标签:
1条回答
  • 2021-01-06 17:44

    In Java, a method can return an abstract class or interface type. What will actually be returned is an object that implements that interface, or extends that abstract class.

    The static method NumberFormat.getCurrencyInstance() will return a concrete object that extends NumberFormat. In the javadoc you can see there are two direct known subclasses: ChoiceFormat and DecimalFormat. There may be more implementations and what is actually returned depends on the implementation of the JVM that you are using.

    0 讨论(0)
提交回复
热议问题