jimao
jimao

Reputation: 55

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?

Upvotes: 4

Views: 7023

Answers (1)

Dan
Dan

Reputation: 198

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.

Upvotes: 6

Related Questions