File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes using abstract method and class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using abstract method and class" Watch "using abstract method and class" New topic
Author

using abstract method and class

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374


1) SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParserFactory is an abstract class. But can we still create an instance of this class using a static method ? By that I mean , can an abstract class be returned from newInstance() ?

2) SAXParser saxParser = factory.newSAXParser();
newSAXParser() is abstract method. Where is this method overridden so that it can return SAXParser ? I don't see it being overridden anywhere in this code.

3) DefaultHandler handler = new DefaultHandler() { _ _ _ _
Is the rest of the code a part of constructor only ? How a method startElement(), in the constructor will be invoked ?

Thanks
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
1. Your understanding is right... Abstract classes are not instantiated. But if you see the API doc for the newInstance() method it says that it instantiates a SAXParserFactory implementation class rather than the abstract SaxParserFactory.
There is also a step by step process listed on how to find which implementation of the SAXParserFactory it should use.

Try running below code and check what is the SAXParserFactory being used by your code.



2. As said above factory.newSAXParser() calls the newSAXParser() method in the underlying SAXParserFactory implementation.

3. It's an anonymous class and not a part of constructor. It's like any other class but you don't specify a class name and directly instantiated with the new keyword.
Please read Creating Anonymous classes and a little bit of information here

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
But if you see the API doc for the newInstance() method it says that it instantiates a SAXParserFactory implementation class rather than the abstract SaxParserFactory.

I din't see SAXParserFactory implementation class anywhere in the API doc. Is this implementation class defined anywhere else ? Can we have abstract class and concrete class by the same name ?

Thanks
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

nirjari patel wrote:
But if you see the API doc for the newInstance() method it says that it instantiates a SAXParserFactory implementation class rather than the abstract SaxParserFactory.

I din't see SAXParserFactory implementation class anywhere in the API doc.


If you read your post, or this one, it appears the forum automatically turns the text SAXParserFactory into a link to the docs.

Can we have abstract class and concrete class by the same name ?


Of course, but they'd have to be in separate packages, and it wouldn't be a very good idea to do so. Just like any two classes or interfaces or class+interface with the same name.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Jeff Verdegan wrote:
Can we have abstract class and concrete class by the same name ?


Of course, but they'd have to be in separate packages, and it wouldn't be a very good idea to do so. Just like any two classes or interfaces or class+interface with the same name.



While this answer, answers the original question, it doesn't addresses the incorrect implication..... Remember that a class that subclasses another class IS-A that class. So, you can have a class that subclass the SAXParserFactory class, and an instance of this class can be returned from the static method, regardless of what the name of the concrete class is.

In terms of implementation, the instance that is returned, is an instance of some concrete class that subclasses the abstract class. And this class is internal -- not exposed via JavaDoc.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Henry Wong wrote:
Jeff Verdegan wrote:
Can we have abstract class and concrete class by the same name ?


Of course, but they'd have to be in separate packages, and it wouldn't be a very good idea to do so. Just like any two classes or interfaces or class+interface with the same name.



While this answer, answers the original question, it doesn't addresses the incorrect implication..... Remember that a class that subclasses another class IS-A that class. So, you can have a class that subclass the SAXParserFactory class, and an instance of this class can be returned from the static method, regardless of what the name of the concrete class is.

Henry


Oops, I guess I missed the point of where his confusion lay. Thanks for clearing it up! :-)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

nirjari patel wrote:
But if you see the API doc for the newInstance() method it says that it instantiates a SAXParserFactory implementation class rather than the abstract SaxParserFactory.

I din't see SAXParserFactory implementation class anywhere in the API doc. Is this implementation class defined anywhere else ? Can we have abstract class and concrete class by the same name ?

Thanks


In addition to what Henry said, you may not find any such class in the docs. The concrete implementation class could be a private nested class, an anonymous inner class, or a package-private class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using abstract method and class