I am curious to know what is the best practice / convention of SE for handling the return type of methods that implement the interface. In particular, suppose we implement a simple tree with an interface as such:
public interface ITreeNode {
public ITreeNode getLeftChild();
public ITreeNode getRightChild();
public ITreeNode getParent();
}
And we have a TreeNode class that implements this:
public class TreeNode implements ITreeNode {
private TreeNode LeftChild, RightChild, Parent;
@Override
public ITreeNode getLeftChild() {
return this.LeftChild;
}
@Override
public ITreeNode getRightChild() {
return this.RightChild;
}
@Override
public ITreeNode getParent() {
return this.Parent;
}
}
My question is: should the return type of the corresponding implemented methods be ITreeNode or TreeNode and why.
Eclipse automatically populates TreeNode methods with the returned ITreeNode type. However, changing it to TreeNode does not cause any errors or warnings even with the @Override flag.
source
share