This code is not mine and i need to add some improvements to it, but i’m stuck in this problem.
I have an abstract class “CallNode” and a lot of subclasses, one of them is “Call”. The “Checker” class is intercepting missed calls, but return them as CallNode. My problem is that i need to get the id of this call, but i can’t access it by CallNode.
Do you have any suggestions to solve this problem?
I let you the code, so that you can better understand the problem:
public abstract class CallNode {
public abstract CallNode hasMissingCall();
}
public class Call extends CallNode {
public int id;
// Simplification of method
public CallNode hasMissingCall() {
if (true)
return this;
// ...
}
}
public class Checker{
private static CallNode rootExpected;
CallNode missing = rootExpected.hasMissingCall();
System.out.println( missing.id ); // THE PROBLEM!!!
}
Thank’s in advance!!!
Define a template method returning the id in the abstract class:
let subclass implement it:
That’s an option. Personally I’ll move the id field upward in the CallNode class, since I think it’s supposed to be unique and shared by all subclasses.