Search code examples
javaobjectinstanceof

Java objects instanceof


The instructions say: Create an equals method that takes an object reference and returns true if the given object equals this object. * Hint: You'll need 'instanceof' and cast to a (Position)

I have:

class Position {

    private double x,y;
    private int id;

public boolean instanceOf(Object a)
    {
        boolean isInstance;
        if ((Position)a instanceof Position)
            isInstance=true;
        else
            isInstance=false;
        return isInstance;
    }

But I'm doing it wrong, and I dont know what my problem is. I'm new to objects so this is kind of confusing me..


Solution

  • As this is a homework question, I won't give a detail answer. You have to implement a equals() method, not a instanceOf() method. I think this link help:

    The code example in them should be sufficient. Ask again if you need more hint.