1

I am making a tree based AI for a game originating in Nepal called Tigers and Goats (or Tigers and Sheep). I am now starting to make the classes for the trees, but I am running into an error where my constructors are the same, although they are using different types of list.

Here are my two constructors:

public MoveTree(List<MoveTree> children, MoveTree parent)
{
    this.children = children;
    this.parent = parent;
}
public MoveTree(List<Move> moves, MoveTree parent)
{
    this.moves = moves;
    this.parent = parent;
}

I am using intellij and it is giving me the error shown here 'MoveTree(List, MoveTree)' clashes with 'MoveTree(List, MoveTree)'; both methods have same erasure

How can I avoid this error while still having my two constructors? I want to be able to do this without changing my constructors too much so that I can have different ways of implementing this class for different purposes

BossCode
  • 71
  • 9
  • Does this answer your question? [Overloading a method: both methods have same erasure](https://stackoverflow.com/questions/43442515/overloading-a-method-both-methods-have-same-erasure) – Giorgi Tsiklauri Mar 24 '21 at 22:51

1 Answers1

1

You can't have both. Use the builder pattern (formal style - not shown here), or a factory method (easier - shown):

private MoveTree(MoveTree parent) {
    this.parent = parent;
}

public static MoveTree createWithMoveTree(List<MoveTree> children, MoveTree parent) {
    MoveTree moveTree = new MoveTree(parent);
    moveTree.children = children;
    return moveTree;
}

public static MoveTree createWithMoves(List<Move> moves, MoveTree parent) {
    MoveTree moveTree = new MoveTree(parent);
    moveTree.moves = moves;
    return moveTree;
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722