CompareTo method of the Comparable interface allows encapsulation breaking?

1

The Student class has its private attributes and to access or modify them requires the use of the get and set methods. In addition, the Student class implements the Comparable interface.

class Estudante implements Comparable<Estudante> {

    private String nome;
    private double nota;

    public Estudante(String nome, double nota) {
        this.nome = nome;
        this.nota = nota;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public double getNota() {
        return nota;
    }

    public void setNota(double nota) {
        this.nota = nota;
    }

    @Override
    public int compareTo(Estudante o) {
        o.nome = "QUEBRA DE ENCAPSULAMENTO";
        return (int) (this.nota - o.getNota());
    }
}

In the main method of the main class TestUniversity are instantiated 2 objects of class Student and the getNome method of object e2 is called before and after the comparison of e1 with e2.

Why is it possible to break the encapsulation of the e2 name attribute into the compareTo method of e1?

See: link

    
asked by anonymous 11.06.2017 / 21:00

0 answers