Google Answers Logo
View Question
 
Q: Java inheritance issue ( No Answer,   4 Comments )
Question  
Subject: Java inheritance issue
Category: Computers > Programming
Asked by: carbon-ga
List Price: $5.00
Posted: 13 Jul 2005 01:21 PDT
Expires: 26 Jul 2005 20:48 PDT
Question ID: 542944
Tag has methods getElement(), setElement().

TagWithBody extends Tag and has method doBody(). This is natural,
because TagWithBody is a Tag but also needs to be able to do its body.

XhtmlTag extends Tag and has methods setId(), setClass(). This is
natural because XhtmlTag is a Tag but needs a couple of additional
setter methods.

Here is the problem:
XhtmlTagWithBody needs getElement(), setElement(), doBody(), setId(),
setClass(). It's as if XhtmlTagWithBody should extend TagWithBody and
XhtmlTag, but can't. XhtmlTagWithBody can't reuse the work done in
XhtmlTag, because XhtmlTag extended Tag instead of TagWithBody.

Please suggest better ways to get this idea accomplished.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Java inheritance issue
From: secret901-ga on 13 Jul 2005 01:33 PDT
 
Java can't extend multiple classes but it can implement more than one
interface.  You can create interfaces for XhtmlTag and TagWithBody
(both extending Tag) and have XhtmlTagWithBody implement both.

See http://java.sun.com/docs/books/tutorial/java/interpack/usinginterface.html

secret901-ga
Subject: Re: Java inheritance issue
From: esolution-ga on 14 Jul 2005 21:07 PDT
 
Instead of trying to use Inheritance, you can use Composition.
XhtmlTagBody can have Xhtml as composition and inherit from
TagWithBody. By doing so you can reuse XhtmlTag by composition and
doBody by Inheritance.
Subject: Re: Java inheritance issue
From: manjax-ga on 22 Jul 2005 04:33 PDT
 
The better option would be to create your Own class and extend the Tab
Body Interface...Create a Static method in it..Call this method
directtly from whereever you require
Subject: Re: Java inheritance issue
From: carbon-ga on 26 Jul 2005 20:48 PDT
 
I solved this to my satisfaction by not being so granular with the
classes. I made a Tag class that had doBody(). Some subclasses don't
make use of it, but that's OK.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy