Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Generics and ClassCastException in Generalization
    primarykey
    data
    text
    <p>It might not be related, but this is under Android 2.3.3 and I work with openjdk7.</p> <p>I'm trying to compute the velocity and displacement of an object using its acceleration values, given coordinates in 3 spaces plus time. For that, I created a class named <code>Coord4D</code>, and <code>Velocity</code>, <code>Displacement</code> and <code>Acceleration</code> are child of this class. Like this:</p> <p><code>Coord4D</code>:</p> <pre><code>class Coord4D { // Statics // private static final long ONE_TO_NANO_FACTOR = 1000000000; // Members // private long key; private float xValue; private float yValue; private float zValue; // ... more ... } </code></pre> <p>and an example of inheriting class <code>Velocity</code>:</p> <pre><code>public class Velocity extends Coord4D{ // ... members and methods and stuff ... } </code></pre> <p>And now the problem. In <code>Coord4D</code>, I generalize the method to obtain an antiderivative from derivatives. For instance, I want to retrieve a <code>Velocity</code> instance given two <code>Acceleration</code> instances (past and current) and the <code>Velocity</code> instance from a previous frame.</p> <p>Everything was working fine when the methods to do this were in <code>Displacement</code> and <code>Velocity</code>, but I found it was redundant since both were computing the exact same things, and I hate writing two times the same lines of code. So I decided to move these methods into <code>Coord4D</code>, and to use generics like this:</p> <pre><code>protected static &lt;Derivative extends Coord4D, AntiDerivative extends Coord4D&gt; AntiDerivative getIntegrationStep( Derivative previous, Derivative current, AntiDerivative previousStep){ // Time is in nanoseconds, must change it in seconds float dT = ( current.getTime() - previous.getTime() ) / ONE_TO_NANO_FACTOR; // Parameters have seconds for unit : ..., ?*s, ?, ?/s, ?/s², ... float dX = current.getX() - previous.getX(); float dY = current.getY() - previous.getY(); float dZ = current.getZ() - previous.getZ(); // do the integration step and add the previous step value float x = dX * dT + previousStep.getX(); float y = dY * dT + previousStep.getY(); float z = dZ * dT + previousStep.getZ(); return (AntiDerivative) new Coord4D(current.getTime(), x, y, z); } </code></pre> <p>and the <code>Velocity</code> and <code>Displacement</code> objects have this method (example from <code>Velocity</code>).</p> <pre><code>public static Velocity getVelocity(Acceleration previousAccel, Acceleration currentAccel, Velocity initialVelo) { return getIntegrationStep(previousAccel, currentAccel, initialVelo); } </code></pre> <p>Now, for some reason, this line </p> <pre><code>return getIntegrationStep(previousAccel, currentAccel, initialVelo); </code></pre> <p>result in a <code>ClassCastException</code>. I don't understand why, as I think my logic with the generics is fine. Can someone help me finding where is the flaw?</p> <p>The stack trace (from LogCat):</p> <pre><code>W/dalvikvm(8891): threadid=1: thread exiting with uncaught exception (group=0x4001d648) E/AndroidRuntime(8891): FATAL EXCEPTION: main E/AndroidRuntime(8891): java.lang.ClassCastException: me.aybabt.android.prototypes.physics.Coord4D E/AndroidRuntime(8891): at me.aybabt.android.prototypes.physics.Velocity.getVelocity(Velocity.java:40) E/AndroidRuntime(8891): at me.aybabt.android.prototypes.AcceleratorActivity.onSensorChanged(AcceleratorActivity.java:199) E/AndroidRuntime(8891): at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:539) E/AndroidRuntime(8891): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(8891): at android.os.Looper.loop(Looper.java:150) E/AndroidRuntime(8891): at android.app.ActivityThread.main(ActivityThread.java:4293) E/AndroidRuntime(8891): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(8891): at java.lang.reflect.Method.invoke(Method.java:507) E/AndroidRuntime(8891): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) E/AndroidRuntime(8891): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) E/AndroidRuntime(8891): at dalvik.system.NativeStart.main(Native Method) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload