| Article: |
Understanding JAXB: Java Binding Customization | |
| Subject: | Polymorphic behaviour problem | |
| Date: | 2003-12-25 14:58:18 | |
| From: | anonymous2 | |
|
Hi, I still did not understand how you would achieve polymorphism. Say you have an external class Shape like public class Shape { protected Color color ; protected Color getColor() { return color ; } protected void setColor(Color color) { ... } }
|
||
Showing messages 1 through 1 of 1.
-
Polymorphic behaviour problem
2003-12-26 12:32:10 hashimisayed [View]



I understand your point. Even if you derive from a common base class that only allows you to say shape.draw() without having to know the particular derived shape type. But what happens when draw is called on the shape? The base class of all shapes can define a draw method but the implementation of the shapes have to implement the method. The problem is that JAXB generates the implementation. The solution is to provide a base interface for all the shapes and extend the JAXB generated implementation classes with your own custom implementations. Let me give you the details. I'll keep using the Shape example.
So,
************* Step (1) ****************
</p>************ Step (2) *****************
<p>
// use JAXB to generated interfaces and classes as usual.
// Now you need to write the implementation classes. The implementation classes need to implement MyShape and extend the JAXB generated implementation class. Here is an example for the Square.
// at this point, you have a base interface and JAXB has generated
// interfaces and implementations for Circle, Rectangle, and Square.
// Now when iterating through the shapes, we can simple call draw().
sayed