Skip to content
Merged
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
42 changes: 18 additions & 24 deletions source/ch6_definingclasses.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,26 @@ public class Fraction {
}
</code> <tests> </tests>
</program>
</listing>
<p>
If you ran <xref ref="java-full-fraction-class" text="type-global"/>, 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>

<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.
Just like in Python, whenever an object is printed by the <c>println</c> method it must be converted to string format.
In Python you can control how that looks by writing an <c>__str__</c> method for your class.
If you do not then you will get the default, which looks something like <xref ref="java-no-friendly-output" text="type-global"/>. We will see how to provide a friendly string representation for our <c>Fraction</c> class in <xref ref="inheritance" text="type-global"/>.
</p>

<exercise label="java_method_overloading_parsons">
<statement>
<p>
Expand Down Expand Up @@ -672,35 +690,11 @@ public class Fraction {
</exercise>

</subsection>


</section>

<section xml:id="inheritance">
<title>Inheritance</title>

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

<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.
Just like in Python, whenever an object is printed by the <c>println</c> method it must be converted to string format.
In Python you can control how that looks by writing an <c>__str__</c> method for your class.
If you do not then you will get the default, which looks something like the above.
</p>
</introduction>


<subsection>
<title>The <c>Object</c> Class</title>

Expand Down