Q:

How do I correctly Override in Java?

In my superclass, I have the following method:
public int getSpeed(String t)
{
    return 0;
}

In my subclass I them overide the method with this:
public int getSpeed(String t)
{
    return x;
}

I then have the following:
ArrayList<super> //contains only objects of the subclass
for (super s:collection)
{
    s.getSpeed("");
}

And this always returns 0. How do I get it to return x?

EDIT: My code was written almost exactly as Bala R's solution shows, however I just did something stupid to my X that caused it to round to 0 every time. His solution therefore is correct.

A:

Maybe your x in the subclass is actually zero or not initialized ? 
I just tried ( Ideone link )
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) throws java.lang.Exception {

        new Main();
    }

    Main(){
        ArrayList<Super> list = new ArrayList<Super>();
        list.add(new Sub());
        list.add(new Sub());
        list.add(new Sub());
        for (Super s:list)
        {
            System.out.println(s.getSpeed(""));
        }
    }
}

class Super {
    public int getSpeed(String t) {
        return 0;
    }
}

class Sub extends Super {
    @Override
    public int getSpeed(String t) {
        return 1;
    }

}

and the output is
1
1
1

