简体   繁体   中英

can we make instance of abstract class in java

Sorry for asking, The Fact we know that in java you cannot create an instance of an abstract class because it does not have a complete implementation. The purpose of an abstract class is to function as a base for subclasses. Then about NumberFormat class in java is abstract right??

the question is how about this code:

NumberFormat numberFormatter = NumberFormat.getNumberInstance();
  1. what does this mean? if im not wrong, this mean that we make an object instance of NumberFormat using numberFormatter? or what?
  2. NumberFormater is abstract right? so we need to make subclass of it.
  3. if no.1 question i ask is false fact, then why we use numberFormatter.format(x); on this code line String numberStr = numberFormatter.format(x);

the full code is shown bellow, Thankyou very much for your response.

public class NumberFormatToy {   
    public static void main(String args[]) {      
        double x = 1000.0/3.0;      
        NumberFormat numberFormatter = NumberFormat.getNumberInstance();      
        NumberFormat currFormatter = NumberFormat.getCurrencyInstance();      
        NumberFormat percentFormatter = NumberFormat.getPercentInstance();      

        String numberStr = numberFormatter.format(x);      
        String currStr = currFormatter.format(x);       
        String percentStr = percentFormatter.format(x);      

        System.out.println("double 1000.0/3.0 berformat number   : "+ numberStr);      
        System.out.println("double 1000.0/3.0 berformat currency : "+ currStr);      
        System.out.println("double 1000.0/3.0 berformat percent  : "+ percentStr);   
    }
}

As you already understand, since NumberFormat is abstract it is not possible to do this:

new NumberFormat();

However, the actual object that is created can (and often does) differ from the type of the variable that references that object. In one of your examples - NumberFormat numberFormatter = NumberFormat.getNumberInstance(); – the object that is returned by NumberFormat.getNumberInstance() is something which extends NumberFormat .

This line of code prints out the class of the object returned by NumberFormat.getNumberInstance() :

System.out.println(NumberFormat.getNumberInstance().getClass());

which prints out:

class java.text.DecimalFormat

If you look up the Javadoc for DecimalFormat , you'll see that it extends NumberFormat .

You could also call NumberFormat.getNumberInstance() and store the result using one of the interfaces that NumberFormat implements (you can see those in the Javadoc ). Here's an example showing how to create a variable of type Serializable :

Serializable s = NumberFormat.getNumberInstance();

and similarly, it is not valid to create an instance of Serializable using a constructor, so this isn't allowed: new Serializable() .

The NumberFormat class is indeed abstract but static methods cannot be abstract.

So, you can define a static method in your abstract class that returns objects of classes that inherit from your abstract class.

The methods getNumberInstance , getCurrencyInstance , getPercentInstance , etc are all static methods that return an object of some class that extends NumberFormat .

If you use a code like that - Collection list = Collections.asList("a", "b"); that means that you get object of interface Collection? Of cause no. This only means that static method asList() returns something that implements Collection. So, if you will see a code like that, be sure that a method return object of some class and if you want to know which one then just CTRL + click on this method and your IDE show you code of this one and you will see what it return.

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