简体   繁体   中英

Abstract class instantiate by own static method

DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); // 

DocumentBuilderFactory is abstract class. newInstance is static method. Can't figure out how it works. What happens?

I mean why it works... If I'm not mistaken we can't make instance of Abstract class.

You don't have an object of the DocumentBuilderFactory on the right side of the "=".

This is a factory method to create a DocumentBuilderFactory outside a constructor.

this looks like some kind of singleton pattern, does the newInstance method return the EXACT same type, or does it return an actual concrete class that extends DocumentBuilderfactory? Is it really abstract, or does it have a private constructor?

DocumentBuilderFactory.newInstance() returns an instance of a concrete subclass of DocumentBuilderFactory. You can read its source here .

newInstance is a static method, not a constructor, so it is free to create an instance of a concrete subtype of DocumentBuilderFactory. Which it does. I don't know the implementation, but I assume it instantiates the default implementation of this class.

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