0

I found the following code. But I can't undestand what it is. Looks like inner class. But seems very strange. What is is? How can we define class and create it? As I know here must be inheritance from DefaultHandler, but where is it?

DefaultHandler handler = new DefaultHandler() {
boolean bfname = false;
...

public void startElement(String uri, String localName,String qName, 
    Attributes attributes) throws SAXException {
...
}

public void endElement(String uri, String localName,
   String qName) throws SAXException {
... 
}
 ... 
};

3 Answers3

1

What you're looking at is an Anonymous Inner Class. They are usually one-shot implementations that aren't expected to be reused. For example, an event handler for a button that is tightly coupled to what that button is doing (unlikely to see reuse unless there are a bunch of buttons that do the same thing).

Using this pattern, you can instantiate an interface or an abstract class and provide the required implementations for interface methods or abstract methods, inline.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
0

It's an anonymous class. Anonymous in that you are defining custom functionality but not in a defined class that can be reused.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
0

Vivin Paliath had a great explaination for Anonymous Inner Classes so I won't repeat it here.

DefaultHandler class is apart of a SAX2 I just had some resources that seemed applicable and useful.

For more about the DefaultHandler class go to: http://docs.oracle.com/javase/1.4.2/docs/api/org/xml/sax/helpers/DefaultHandler.html

For more about SAX handlers: http://oreilly.com/catalog/sax2/chapter/ch03.html

BadgerAndK
  • 44
  • 3