简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM