title: How to convert between "similar" 3D-vector classes in java?
body: <p>I'm writing a geom package as part of a Java library that has its own vector class:</p>
  
  <pre class="lang-java prettyprint-override"><code>package mypackage.geom;
  
  public class Vector3D {
    public float x;
    public float y;
    public float z;
  
    public Vector3D(float x, float y, float z) {
      this.x = x;
      this.y = y;
      this.z = z;
    }
    ...
  }
  </code></pre>
  
  <p>My library is expected to complement the functionality of a third party library which also implements a vector class, say <code>Vec3D</code>. The <code>Vec3D</code> class has the same fields (<code>float x</code>, <code>float y</code> and <code>float z</code>) and constructor signatures as my <code>Vector3D</code> class. I just want to use the third party vector class everywhere a <code>Vector3D</code> parameter is expected in my library (e.g., Suppose I have a <code>Camera</code> class that has a <code>setPosition(Vector3D vector3D)</code> method, and I want be able to pass a <code>Vec3D</code> instance to it).</p>
  
  <p>This is possible in C++. For instance, one can declare an universal explicit converter constructor from any class to Vector3D, in the following way:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>template &lt;class C&gt;
  explicit Vector3D(const C&amp; c) : x(c[0]), y(c[1]), z(c[2]) {}
  </code></pre>
  
  <p>all that it requires is that the foreigner class implements the <code>operator[]</code>. I was wondering how to implement something similar in Java, if possible at all.</p>