I'll get straight into it. Below is a code snippet from my GeneralSearch class I am implementing in an AI application for solving a word puzzle. I am wondering how to instantiate the subclass within the abstract class. I feel I have implemented all relevant abstract methods in my BreadthFirstSearch class as well as its constructor calling its super-constructor. (Nested subclass)
I understand you cant instantiate an abstract class but from previous threads and information you can instantiate a subclass with an abstract super-class.
I try the following calls to no avail:
GeneralSearch bfs = new BreadthFirstSearch(now.getState(), info); //Correct parameters
OR.
GeneralSearch bfs = GeneralSearch.BreadthFirstSearch(now.getState(), info);
Errors received: 1)BreadthFirstSearch cannot be resolved to a type 2)The method BreadthFirstSearch(WordState, WordNodeInfo) is undefined for the type GeneralSearch
I cant seem to get a working instantiation for the subclass so I can perform such searches. If anyone could shed some light on my confusion and understanding that would be great. (Code below)
public abstract class GeneralSearch {
NodeInfo nodeInfo;
ArrayList unvisited, visited;
public GeneralSearch (State startState, NodeInfo nodeInfo) {
this.nodeInfo = nodeInfo;
unvisited = new ArrayList();
unvisited.add(new Node(startState, new Actions()));
visited = new ArrayList();
}
public Node search() {
Actions moves;
Action move;
//Iterating through arrayList for unvisited and possible arcs
Node visit, successor;
if(unvisited.isEmpty()) return null;
while( !unvisited.isEmpty() ) {
visit = select();
if(nodeInfo.isGoal(visit)) return visit;
moves = visit.getState().getActions();
Iterator<Action> it = moves.iterator();
while(it.hasNext()) {
successor = (Node) visit.clone();
move = it.next();
successor.getState().update(move);
insert(successor);
}
visited.add(visit);
}
return null;
}
public abstract Node select ();
public abstract void insert (Node node);
public class BreadthFirstSearch extends GeneralSearch {
public BreadthFirstSearch(State startState, NodeInfo nodeInfo) {
super(startState, nodeInfo);
}
public Node select() {
return (Node) visited.get(0);
}
public void insert(Node node) {
unvisited.add(node);
}
}
}