Salve a tutti, penso proprio che questo problema che ho sia sui fondamentali della programmazione ad oggetti. In questo esercizio non riesco proprio a capire il funzionamento del metodo getLength .



codice:

class Point {


    private int x;
    private int y;


    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }


    public static void main(String argv[]) {


        Point p1 = new Point(0, 0);
        Walk w1 = new Right(new Down(new Left(new Up(new Stop()))));
        Walk w2 = new Left(new Left(new Up(new Stop())));
        // Walk w3=new Right(new Stop());
        System.out.println(w2.getLength());
    }
}


abstract class Walk {


    public abstract boolean isStop();


    public abstract int getLength();
}


class Stop extends Walk {


    @Override
    public boolean isStop() {
        return true;
    }


    @Override
    public int getLength() {
        return 0;
    }
}


abstract class Move extends Walk {


    Walk tail;
    


    @Override
    public int getLength() {
      
       return 1+tail.getLength();
    }


    Move(Walk tail) {
        this.tail = tail;
        System.out.println(tail);
    }


    @Override
    public boolean isStop() {
        return true;
    }
}


class Right extends Move {


    public Right(Walk tail) {


        super(tail);


    }
}


class Left extends Move {


    public Left(Walk tail) {
        super(tail);
    }
}


class Up extends Move {


    public Up(Walk tail) {
        super(tail);
    }
}


class Down extends Move {


    public Down(Walk tail) {
        super(tail);
    }
}