Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using abstract method and class

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic