• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Inner class and multiple inheritance

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I love a good mentalist. And so does this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic