Ani Ani - 5 months ago 11
Java Question

Parent/Child class relationship - indexOf

My question is very specific. I'm working with a parent and a child class. I'm working with specific given instructions and I'm to override the indexOf() method from the parent class in the child's class. If the parameter is a parent's class object, return the parent class’s indexOf() value based on the parameter value. If the parameter is a String object, return the parent class’s indexOf() value when it is passed a parent's class object created from the string parameter.

The bolded parts are what I'm not getting. This is the first time I'm working with this sort of thing so I'm not sure how to approach it. This is what I have so far:

public class KillList extends LinkedList<AssassinNodeData> {

public int indexOf(Object data) {
if (data instanceof AssassinNodeData) {
return .....
} else if (data instanceof String) {
return ....
} else
return -1; // under all other circumstances
// circumstance to indicate parameter is not of current class
}
}


Any suggestions or solutions would be wonderful!

On the topic of parent/child class. As I'm extending with LinkedList instead of just extending the superclass - is there a special way in which I access the getter/setter methods from the superclass?

Here's an example of what I'm working on. The compiler is giving me the "Cannot Find Symbol" and that the location of the getter method is in subclass, but not the superclass:

@Override
public String toString() {
return getKiller() + "is stalking" + getPlayer();
}

Answer

You need to use the super keyword here to access the parent's indexOf:

public int indexOf(Object data) {
    if (data instanceof AssassinNodeData) {
        return super.indexOf(data);
    } else if (data instanceof String) {
        return super.indexOf(new AssassinNodeData((String)data));
    } else {
        return -1;
    }
}