Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a method return an abstract class type?

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

What does it mean?

I am a little confused because I know that we can create an object of an abstract class. So this method returns an object and if this method doesn't return an object what does it return?

like image 983
jimao Avatar asked Nov 04 '25 01:11

jimao


1 Answers

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.

like image 68
Dan Avatar answered Nov 06 '25 17:11

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!