1

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);
      }
  }

}
Alistair Gillespie
  • 540
  • 1
  • 5
  • 22

1 Answers1

1

Put the BreadthFirstSearch class in a separate file, outside of the GeneralSearch class.

If you want to keep it there, try instantiating GeneralSearch.BreadthFirstSearch instead of just BreadthFirstSearch, but I'm not even sure it's possible to have a subclass within it's own parent.

In your second try you're missing a new statement:

GeneralSearch bfs = new GeneralSearch.BreadthFirstSearch(now.getState(), info);
tbkn23
  • 5,205
  • 8
  • 26
  • 46
  • I thought nested subclasses could work... Ill try creating individual classes for all of my subclasses and see how that goes.. – Alistair Gillespie Apr 19 '13 at 12:40
  • Ok, so I General Search bfs = new BreadthFirstSearch(....); works when I separated all the subclasses from the GeneralSearch folder... I thought this may have been good coding practice to nest the subclasses. Obviously not... – Alistair Gillespie Apr 19 '13 at 12:49
  • 1
    I checked some more. Apparently it is possible to have a subclass inherit from its enclosing class, but then to instantiate it you would need to create an instance of the enclosing class, and then use instance.new to instantiate the inner class. Of course this won't work in the enclosing class is abstract since you can't instantiate it... – tbkn23 Apr 19 '13 at 12:52
  • 1
    Ok, I was intrigued... this post might be of some help if you still want to do it with nested classes: http://stackoverflow.com/questions/2863157/how-does-object-new-work-does-java-have-a-new-operator – tbkn23 Apr 19 '13 at 12:56