1
public boolean equals(Object o) { 
  if( !(o instanceof TypedValue)){
    return false;

  }
  else{
     if (o.type() == this.type()){
    //stuff
   }

}

(im a java noob) this code is from my TypedValue class, I get an error on the o.type() as type() isnt a method of object. But type() is a method of Typedvalue and o must be an instance of TypedValue. I can cast to remove the error but im not sure if my code will work in the correct way. Its important to the rest of the code that the function take an object as its parameter and not just a typedvalue

1
  • as @JB Nizet mentioned, casting is safe and this is the place to do it. It works because of the parent child relationship, a Parent can be an instance of the child, in your case TypeValue is the child class of object, and since you validate that it is indeed TypeValue, you can cast it safely Commented Mar 3, 2016 at 17:45

1 Answer 1

4

The answer is in the question: you must cast.

The cast is safe, since you just checked that o was indeed an instance of TypedValue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.