title: Java compare class instances using just interface methods
body: <p>My application uses interfaces as access control in order to mask some database-specific fields from the application. For example, the <code>PublicFoo</code> interface exposes a subset of data from the <code>DatabaseFoo</code> class.</p>
  <pre class="lang-java prettyprint-override"><code>public interface PublicFoo() {
    void setName(String name);
    String getName();
    void setId(String id);
    String getId();
  }
  
  public class DatabaseFoo() implements PublicFoo {
    private String name;
    private String id;
  
    // I want to hide this database-specific field from the PublicFoo interface that gets passed around my app. 
    private OffsetDateTime lastUpdatedTimestamp;
  }
  </code></pre>
  <p>My question is, if I have two instances of the class <code>DatabaseFoo</code> that implement the <code>PublicFoo</code> interface, is there a way to compare these two instances using only the attributes exposed by the getters of the interface? I don't want to override the <code>equals()</code> method of <code>DatabaseFoo</code> because there are cases where I want to compare two <code>DatabaseFoo</code> objects using every field. So I think this rules out default interface methods or abstract classes.</p>
  <p>I can accomplish what I want by writing a custom method that calls all the getters and checks for equivalency. However I am wondering if there is a more elegant solution. My fear is that if I use this approach I won't remember to update the custom method as the interface evolves in the future.</p>