diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index c1b2d6f..b2a66fa 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -462,15 +462,16 @@ public class Fraction {

- If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like this: + If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like .

- - + + Fraction@6ff3c5b5 +

The reason is that we have not yet provided a friendly string representation for our Fraction objects. @@ -487,7 +488,7 @@ Fraction@6ff3c5b5

toString In Java, the equivalent of __str__ is the toString method. - Every object in Java already has a toString method defined for it because every class in Java automatically inherits from the Object class. + Every object in Java already has a toString method defined for it because every class in Java automatically inherits from the Object class. The Object class provides default implementations for the following methods.

@@ -551,20 +552,21 @@ Fraction@6ff3c5b5

We are not interested in most of the methods on that list, and many Java programmers live happy and productive lives without knowing much about most of the methods on that list. - However, to make our output nicer we will implement the toString method for the Fraction class. - A simple version of the method is provided below. + However, to make our output nicer we will implement the toString method for the Fraction class. shows a simple version of the method.

- - + + public String toString() { return numerator.toString() + "/" + denominator.toString(); } +

+ equals The other important class for us to implement from the list of methods inherited from Object is the equals method. In Java, when two objects are compared using the == operator they are tested to see if they are exactly the same object (that is, do the two objects occupy the same exact space in the computer’s memory?). This is also the default behavior of the equals method provided by Object. @@ -573,30 +575,32 @@ public String toString() { Therefore once you write your own equals method:

- - + + object1 == object2 +

- is NOT the same as: + is NOT the same as .

- - + + object1.equals(object2) +

- Here is an equals method for the Fraction class: + is an equals method for the Fraction class.

- - + + public boolean equals(Fraction other) { Integer num1 = this.numerator * other.getDenominator(); @@ -608,6 +612,7 @@ public boolean equals(Fraction other) { } +

One important thing to remember about equals is that it only checks to see if two objects are equal – it does not have any notion of less than or greater than. @@ -631,21 +636,22 @@ public boolean equals(Fraction other) {

- Here is code that makes the Fraction class a child of Number: + makes the Fraction class a child of Number.

- - + + public class Fraction extends Number { ... } +

extends - The keyword extends tells the compiler that the class Fraction extends, or adds new functionality to the Number class. + The keyword extends tells the compiler that the class Fraction extends, or adds new functionality to the Number class. A child class always extends its parent.

@@ -682,11 +688,11 @@ public class Fraction extends Number {

- This really isn’t much work for us to implement these methods, as all we have to do is some type conversion and some division: + This really isn’t much work for us to implement these methods, as all we have to do is some type conversion and some division as shown in .

- - + + public double doubleValue() { return numerator.doubleValue() / denominator.doubleValue(); @@ -702,6 +708,7 @@ public long longValue() { } +

is-a @@ -717,17 +724,18 @@ public long longValue() {

- Suppose you try to define a method as follows: + Suppose you try to define a method as .

- - + + public void test(Number a, Number b) { a.add(b); } +

The Java compiler would give an error because add is not a defined method of the Number class.