Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 34 additions & 26 deletions source/ch6_definingclasses.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -462,15 +462,16 @@ public class Fraction {

<introduction>
<p>
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 <xref ref="java-no-friendly-output" text="type-global"/>.
</p>


<program xml:id="java-no-friendly-output" language="java">
<listing xml:id="java-no-friendly-output">
<program language="java">
<code>
Fraction@6ff3c5b5
</code>
</program>
</listing>

<p>
The reason is that we have not yet provided a friendly string representation for our <c>Fraction</c> objects.
Expand All @@ -487,7 +488,7 @@ Fraction@6ff3c5b5
<p>
<idx><c>toString</c></idx>
In Java, the equivalent of <c>__str__</c> is the <c>toString</c> method.
Every object in Java already has a <c>toString</c> method defined for it because every class in Java automatically inherits from the <c>Object</c> class.
Every object in Java already has a <term> <c>toString</c> method</term> defined for it because every class in Java automatically inherits from the <c>Object</c> class.
The <c>Object</c> class provides default implementations for the following methods.
</p>

Expand Down Expand Up @@ -551,20 +552,21 @@ Fraction@6ff3c5b5

<p>
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 <c>toString</c> method for the <c>Fraction</c> class.
A simple version of the method is provided below.
However, to make our output nicer we will implement the <c>toString</c> method for the <c>Fraction</c> class. <xref ref="java-tostring" text="type-global"/> shows a simple version of the method.
</p>


<program xml:id="java-tostring" language="java">
<listing xml:id="java-tostring">
<program language="java">
<code>
public String toString() {
return numerator.toString() + "/" + denominator.toString();
}
</code>
</program>
</listing>

<p>
<idx><c>equals</c></idx>
The other important class for us to implement from the list of methods inherited from <c>Object</c> is the <c>equals</c> method.
In Java, when two objects are compared using the <c>==</c> 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&#x2019;s memory?).
This is also the default behavior of the <c>equals</c> method provided by <c>Object</c>.
Expand All @@ -573,30 +575,32 @@ public String toString() {
Therefore once you write your own <c>equals</c> method:
</p>


<program xml:id="java-class-equalqqual" language="java">
<listing xml:id="java-class-equal-equal">
<program>
<code>
object1 == object2
</code>
</program>
</listing>

<p>
is NOT the same as:
<xref ref="java-class-equal-equal" text="type-global"/> is NOT the same as <xref ref="java-class-dot-equals" text="type-global"/>.
</p>


<program xml:id="java-class-dot-equals" language="java">
<listing xml:id="java-class-dot-equals">
<program>
<code>
object1.equals(object2)
</code>
</program>
</listing>

<p>
Here is an <c>equals</c> method for the <c>Fraction</c> class:
<xref ref="java-fraction-equals" text="type-global"/> is an <c>equals</c> method for the <c>Fraction</c> class.
</p>


<program xml:id="java-fraction-equals" language="java">
<listing xml:id="java-fraction-equals">
<program language="java">
<code>
public boolean equals(Fraction other) {
Integer num1 = this.numerator * other.getDenominator();
Expand All @@ -608,6 +612,7 @@ public boolean equals(Fraction other) {
}
</code>
</program>
</listing>

<p>
One important thing to remember about <c>equals</c> is that it only checks to see if two objects are equal &#x2013; it does not have any notion of less than or greater than.
Expand All @@ -631,21 +636,22 @@ public boolean equals(Fraction other) {
</p>

<p>
Here is code that makes the <c>Fraction</c> class a child of <c>Number</c>:
<xref ref="java-class-extends" text="type-global"/> makes the <c>Fraction</c> class a child of <c>Number</c>.
</p>


<program xml:id="pjava-extends" language="java">
<listing xml:id="java-class-extends">
<program>
<code>
public class Fraction extends Number {
...
}
</code>
</program>
</listing>

<p>
<idx><c>extends</c></idx>
The keyword <c>extends</c> tells the compiler that the class <c>Fraction</c> extends, or adds new functionality to the <c>Number</c> class.
The keyword <term><c>extends</c></term> tells the compiler that the class <c>Fraction</c> extends, or adds new functionality to the <c>Number</c> class.
A child class always extends its parent.
</p>

Expand Down Expand Up @@ -682,11 +688,11 @@ public class Fraction extends Number {
</p>

<p>
This really isn&#x2019;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&#x2019;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 <xref ref="java-object-type-conversions" text="type-global"/>.
</p>


<program xml:id="java-object-type-conversions" language="java">
<listing xml:id="java-object-type-conversions">
<program language="java">
<code>
public double doubleValue() {
return numerator.doubleValue() / denominator.doubleValue();
Expand All @@ -702,6 +708,7 @@ public long longValue() {
}
</code>
</program>
</listing>

<p>
<idx>is-a</idx>
Expand All @@ -717,17 +724,18 @@ public long longValue() {
</p>

<p>
Suppose you try to define a method as follows:
Suppose you try to define a method as <xref ref="java-ugly-add-method" text="type-global"/>.
</p>


<program xml:id="java-ugly-add-method" language="java">
<listing xml:id="java-ugly-add-method">
<program language="java">
<code>
public void test(Number a, Number b) {
a.add(b);
}
</code>
</program>
</listing>

<p>
The Java compiler would give an error because <c>add</c> is not a defined method of the <c>Number</c> class.
Expand Down