1

I am following the example from here:

In the section on Interfaces, it was noted that a class that implements an interface must implement all of the interface's methods. It is possible, however, to define a class that does not implement all of the interface methods, provided that the class is declared to be abstract. For example,

abstract class X implements Y {
  // implements all but one method of Y
}

class XX extends X {
  // implements the remaining method in Y
}

In this case, class X must be abstract because it does not fully implement Y, but class XX does, in fact, implement Y.

Here is my code:

public interface IRunnable {
public Object[] run(Graph<Object, ?> graph, HashMap<Object, Point> positions, int numIter);
public Object[] runSet(Graph<Object, ?> graph, HashMap<Object, Point> positions, int numIter, int [] itr);
public HashMap<Object, Point> runHelper(Graph<Object, ?> graph,HashMap<Object, Point> positions, int numIter);
public Force calculateForces(Graph<Object, ?> graph, HashMap<Object, Point> positions, Object mainVertex);

}

public abstract class Runnable implements IRunnable {
    public Object[] run(Graph<Object, ?> graph, HashMap<Object, Point> positions, int numIter){
           Object[] res = new Object[2];
           long startTime = System.currentTimeMillis();
           res[0] = runHelper(graph, new HashMap<Object, Point>(positions), numIter);
           long endTime = System.currentTimeMillis();
           res[1] = endTime - startTime;
           return res;
        }
        public Object[] runSet(Graph<Object, ?> graph, HashMap<Object, Point> positions, int numIter, int [] itr){
             System.out.println(itr.length);
             Object[] res = new Object[itr.length];
             for ( int i = 0; i < itr.length; i++){
                 res[i] = run(graph, new HashMap<Object, Point>(positions), itr[i]);
             }
             return res;
        }
        public HashMap<Object, Point> runHelper(Graph<Object, ?> graph,HashMap<Object, Point> positions, int numIter){
            Collection<Object> vertices = graph.getVertices();
            HashMap<Object, Force> forces = new HashMap<Object, Force>();
             for ( int i = 0; i < numIter; i++){
                 for ( Object vertex : vertices ){
                     forces.put(vertex, calculateForces(graph, positions, vertex));
                 }
                 for ( Object vertex : vertices ){

                    positions.put(vertex, ForceFunctions.calculateShift(positions.get(vertex), forces.get(vertex)));
                 }
             }
             return positions;
        }
}

public class Eades extends Runnable {



public static Force calculateForces(Graph<Object, ?> graph, HashMap<Object, Point> positions, Object mainVertex){
    ArrayList<Object> vertices = new ArrayList<Object>(graph.getVertices());
    Force totalForce = new Force(0,0);
    Force neighborForce = new Force(0,0);
    Object neighborVertex = null;

    for ( int i = 0; i < vertices.size(); i++){
        neighborForce = null;
        neighborVertex = vertices.get(i);
        if ( mainVertex != neighborVertex ){  
            if ( graph.getNeighbors(mainVertex).contains(neighborVertex) ){
                neighborForce = ForceFunctions.attractive(positions.get(mainVertex),positions.get(neighborVertex));
            }
            else
                neighborForce = ForceFunctions.repulsive(positions.get(mainVertex), positions.get(neighborVertex) );

            totalForce.add(neighborForce);
        }
    }
    return totalForce;
}

But I get an error saying:

The type Runnable cannot be the superclass of Eades; a superclass must be a class

What am I doing wrong?

1 Answer 1

2

Java has a Runnable interface in the java.lang package, which is conflicting with your class. The compiler is thinking that you want to extend from that interface in your Eades declaration.

Rename your class to something else (MyRunnable or something more meaningful), which will take care of the error.

Sign up to request clarification or add additional context in comments.

1 Comment

No, it has Runnable in java.lang - that's not the same as "the default package".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.