• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class and multiple inheritance

 
Harish Tiruvile
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

My Question: to some extent We can overcome multiple inheritance problem using Innerclass..does it have problem with performance?


For example::

I want to read XML data from java servlet program...

To read XML i need to extend DefalutHandler class..& for Servlet i need to extend HttpServlet class...

I want to create only one Class(i have restricted,just to give an example) which will do both task(servlet -> for getting data from user and XML-> getting required data to user )...

Since multiple inheritance is not supported in java i cannot extend both classes in one class..

Innerclass overcome this multiple inheritance problem..

like:

DefaultHandler obj = new DefaultHandler() {
public void startDocument() throws SAXException
{

//implementation
}
public void endDocument() throws SAXException
{
//implementation
}


.
.
.
.
.
.
.

};

Can i consider this as good programing?..I hope i can reuse this code.if any one knows drawback in using Innerclass.....please explain it to me..or else ..post the link..
[ September 14, 2006: Message edited by: harish thrivile ]
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by harish thrivile:

I want to create only one Class(i have restricted,just to give an example) which will do both task(servlet -> for getting data from user and XML-> getting required data to user )...


Regardless of how you slice it - inner class, more than one class in a file; You have created more than one class.

As to performance I doubt this runs much slower than 2 classes in 2 files. When it compiles it will produce 2 class files.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you have done is not multiple inheritance at all. Your outer class still extends only one superclass. Your inner class is a proper class all its own, just defined on the fly. So if we leave "multiple inheritance" out of the conversation ...

Building an inner class like that to handle some task is common enough, especially with event handlers in Swing. I get uncomfortable if they're over a few lines long. Your desire to do two things in one source file works against the single responsibility or single reason to change principle.

For this particular example, a SAX handler is a complex enough beast to be at least one separate class.

Sorry the answer wound up so negative, because the qeustion goes into some of my favorite aspects of design. If you want to delve into such stuff more deeply, scroll on down to the OO, UML, etc. forum. Maybe we can get you fired up about it, too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic