Search code examples
javainterfacecastingimplements

java interface avoid casting


I have two classes. An interface class "Node" and "Nodegraph" that implements "Node". "Node" has one parametric method, add, where I use Node node as paramenter. In NodeGraph, I need to casting (NodeGraph) node in order to use NodeGraph object. Is there a better way to avoid casting?

import java.util.Set;

public interface Node {
    public String getId();
    public void add(Node node);
    public boolean remove(Node node);
}


import java.util.Iterator;
import java.util.LinkedList;

public class NodeGraph implements Node {

    private int row;
    private int column;
    private String id;
    private LinkedList<NodeGraph> neighbourds = new LinkedList<NodeGraph>();

    public NodeGraph(int row, int column) {
        this.row = row;
        this.column = column;
        id = "" + row + column;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public void add(Node node) {
        neighbourds.add((NodeGraph) node);
    }

    @Override
    public boolean remove(Node node) {
        return neighbourds.remove((NodeGraph) node);
    }

}

Solution

  • I assume you have valid reasons to keep neighbourds list bound to NodeGraph type. If so here is how you can avoid casting by parametrizing the Node interface:

    public interface Node< T extends Node<T> > {
        public String getId();
        public void add(T node);
        public boolean remove(T node);
    }
    
    public class NodeGraph implements Node<NodeGraph> {
    
        private int row;
        private int column;
        private String id;
        private LinkedList<NodeGraph> neighbourds = new LinkedList<NodeGraph>();
    
        public NodeGraph(int row, int column) {
            this.row = row;
            this.column = column;
            id = "" + row + column;
        }
    
        @Override
        public String getId() {
            return id;
        }
    
        @Override
        public void add(NodeGraph node) {
            neighbourds.add(node);
        }
    
        @Override
        public boolean remove(NodeGraph node) {
            return neighbourds.remove(node);
        }
    }