title: Comparable interface Q
body: <p>I am a Java beginner, now studying the comparable interface. I meet a question. Could anyone help me? Thanks a lot^_^</p>
  
  <p>Here is the example in my Java video course.
  The author maybe tries to override the compareTo, and compare the area of a shape (It should be a class) to the other shape. (obj)</p>
  
  <pre><code>public abstract class Shape implements Comparable{
      public int compareTo(Object obj){
          final double EPSILON = 1.0e-15; // Slightly bigger than machine precision
          Shape rhs = (Shape)obj;
          double diff = area() - rhs.area();
          if (Math.abs(diff)&lt;=EPSILON*Math.abs(area()))
              return 0; // area of this shape equals to area of obj
          else if (diff&lt;0)
              return -1; // area of this shape less than area of obj
          else 
              return 1; // area of this shape greater than area of obj
      }
  }
  </code></pre>
  
  <p>After reading this, I only have a partial picture of how the Comparable interface works. Therefore, I try to write my own compareTo.</p>
  
  <pre><code>public class BankAccount {
      private String myPassword;
      private double myBalance;
  
      public BankAccount() {
  
      } // default constructor 
  
      public BankAccount(String password, double balance) {
          myPassword = password;
          myBalance = balance;
      } // constructor 
  
      public double compare(Object obj){
          BankAccount theOther= (BankAccount)obj;
          double diff = myBalance() - theOther.myBalance;
          return diff;
      } //the "compareTo" I wrote
  
  }
  </code></pre>
  
  <p>However, the computer says that myBalance is not defined in BankAccount.
  And I delete the "()" after myBalance. </p>
  
  <pre><code>double diff = myBalance - theOther.myBalance;
  </code></pre>
  
  <p>It works.</p>
  
  <p>Therefore my question is why the "()" after myBalance is decisive for whether the program will run correctly. I mean but in the author's example, the "area" is still followed "()".</p>
  
  <p>I guess maybe because the example is only a part of the codes, there is a method <code>private double area(){return area;}</code>hiding behind, and this allows the "area()" works. If I add </p>
  
  <pre><code>private double myBalance() {
      return myBalance;
  }
  </code></pre>
  
  <p>My program will also run correctly with the "BankAccount()" right?
  Thanks a lot. Looking forward someone to reply. ^_^</p>