Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>edit: I just realized that number has <code>doubleValue()</code> as an abstract method in the Number class. Ok this is perfect. Just implement equals in all five classes like this. Alternatively if Number is not required as an abstract class, then read below to see that you can create your own abstract class that implements <code>equals()</code> and save duplicated code.</p> <pre><code>public boolean equals(Object x) { if(x instanceof Number == false) return false; Number other = (Number) x; return other.doubleValue() == doubleValue(); } </code></pre> <p>The reason why you can't do what you are doing in your equals is because you are casting whatever you are trying to compare against to a RationalN class. When this is done to a class that is not a RationalN, this results in a ClassCastException.</p> <p>Since all classes extend Number and <code>doubleValue()</code> is declared as a method available to Number, you can now cast those classes to Number (if they are an instance of Number) and then access the method from there.</p> <p>I hope this makes sense. I encourage you to read up on interfaces and polymorphism.</p> <p>edit: As an aside, if you are not required to extend Number, you can create an abstract class which has the abstract method <code>doubleValue()</code>, and then you can provide a concrete implementation of equals(that I provided above) in the abstract class. Then have all five of your classes extend the abstract class. They would each then implement their own version of <code>doubleValue()</code>, but would share the <code>equals()</code> defined in your abstract class, and thus eliminate copy/paste of the <code>equals()</code> into all five classes. This would be the ideal route if you did not have to extend Number.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload