aspose file tools*
The moose likes Java in General and the fly likes Inner class and  multiple inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inner class and  multiple inheritance " Watch "Inner class and  multiple inheritance " New topic
Author

Inner class and multiple inheritance

Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
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 ]

Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class and multiple inheritance