title: WebSerices (JAX-WS) and Inheritance
body: <p>I would like to return a collection of vehicles, where AbstractVehicle is parent of Car and Motorbyte class.</p>
  
  <p>If I have only</p>
  
  <pre><code>   public Collection&lt;AbstractVehicle&gt; getVehicles() {
            Collection&lt;AbstractVehicle&gt; ret = new ArrayList&lt;AbstractVehicle&gt;();
            ret.add(new Car());
            ret.add(new Motorbyte());
            return ret;
     }
  </code></pre>
  
  <p>I will get result that are only AbstractVehicle.</p>
  
  <pre><code>&lt;S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  &lt;S:Body&gt;
  &lt;ns2:getVehiclesResponse xmlns:ns2="http://webservice.delegator.stp.mimos.my/"&gt;
  &lt;return&gt;
    &lt;weight&gt;0&lt;/weight&gt; 
    &lt;/return&gt;
  &lt;return&gt;
    &lt;weight&gt;0&lt;/weight&gt; 
    &lt;/return&gt;
    &lt;/ns2:getVehiclesResponse&gt;
    &lt;/S:Body&gt;
    &lt;/S:Envelope&gt;
  </code></pre>
  
  <p>If I added a dummy method that contain the child classes, </p>
  
  <pre><code>public boolean setAll(Car car, Motorbyte mb) {
                return true;
         }
  </code></pre>
  
  <p>The getVehicles will return result that are the real child classes.</p>
  
  <pre><code>&lt;S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  &lt;S:Body&gt;
  &lt;ns2:getVehiclesResponse xmlns:ns2="http://webservice.delegator.stp.mimos.my/"&gt;
  &lt;return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:car"&gt;
    &lt;weight&gt;0&lt;/weight&gt; 
    &lt;numOfSaftyBelts&gt;0&lt;/numOfSaftyBelts&gt; 
    &lt;/return&gt;
  &lt;return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:motorbyte"&gt;
    &lt;weight&gt;0&lt;/weight&gt; 
    &lt;/return&gt;
    &lt;/ns2:getVehiclesResponse&gt;
    &lt;/S:Body&gt;
    &lt;/S:Envelope&gt;
  </code></pre>
  
  <p>Why it behaves in this way? Any alternative to get the correct result w/o a dummy method?
  FYI, I am using Metro <a href="http://jax-ws.java.net/" rel="nofollow">http://jax-ws.java.net/</a>. Let me know if you need other info.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>After I added @XmlSeeAlso({Car.class,Motorbyte.class}) on AbstractVehicle class, I am seeing SOAP returns correct type even w/o the dummy method. However, at my client, I got the below error:</p>
  
  <pre><code>Exception in thread "main" com.sun.xml.ws.encoding.soap.DeserializationException: Failed to read a response: javax.xml.bind.UnmarshalException
   - with linked exception:
  [javax.xml.bind.UnmarshalException: Unable to create an instance of my.mimos.stp.delegator.webservice.client.AbstractVehicle
   - with linked exception:
  [java.lang.InstantiationException]]
  </code></pre>
  
  <p>So the issue still open. See similar error on other forums but no one provide answer. :(</p>
  
  <p>Thanks.</p>