You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Once you have identified the instance variables for your class the next thing to consider is the constructor.
221
222
In Java, <term>constructors</term> have the same name as the class and are declared public.
222
223
They are declared without a return type.
@@ -281,8 +282,8 @@ public Fraction(Integer num, Integer den) {
281
282
<li>
282
283
<p>
283
284
<idx>pass-by-value</idx>
284
-
<idx>value of the reference</idx>
285
-
<term>Java is strictly pass-by-value.</term> For primitive types (like <c>int</c>), a copy of the value is passed. For object types (like our <c>Fraction</c>), a copy of the <em><term>value of the reference</term></em> (the memory address) is passed.
285
+
286
+
Java is strictly <term>pass-by-value</term>. For primitive types (like <c>int</c>), a copy of the value is passed. For object types (like our <c>Fraction</c>), a copy of the <term>reference</term>(Namely, the memory address) is passed.
286
287
</p>
287
288
</li>
288
289
<li>
@@ -470,15 +471,16 @@ public class Fraction {
470
471
471
472
<introduction>
472
473
<p>
473
-
If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like this:
474
+
If you ran the program above you probably noticed that the output is not very satisfying. Chances are your output looked something like <xrefref="java-no-friendly-output"text="type-global"/>.
The reason is that we have not yet provided a friendly string representation for our <c>Fraction</c> objects.
@@ -495,7 +497,7 @@ Fraction@6ff3c5b5
495
497
<p>
496
498
<idx><c>toString</c></idx>
497
499
In Java, the equivalent of <c>__str__</c> is the <c>toString</c> method.
498
-
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.
500
+
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.
499
501
The <c>Object</c> class provides default implementations for the following methods.
500
502
</p>
501
503
@@ -559,52 +561,55 @@ Fraction@6ff3c5b5
559
561
560
562
<p>
561
563
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.
562
-
However, to make our output nicer we will implement the <c>toString</c> method for the <c>Fraction</c> class.
563
-
A simple version of the method is provided below.
564
+
However, to make our output nicer we will implement the <c>toString</c> method for the <c>Fraction</c> class. <xrefref="java-tostring"text="type-global"/> shows a simple version of the method.
The other important class for us to implement from the list of methods inherited from <c>Object</c> is the <c>equals</c> method.
577
580
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’s memory?).
578
581
This is also the default behavior of the <c>equals</c> method provided by <c>Object</c>.
579
582
The <c>equals</c> method allows us to decide if two objects are equal by looking at their instance variables.
580
-
However it is important to remember that since Java does not have operator overloading <term>if you want to use your</term> <c>equals</c> <term>method you must call it directly</term>.
583
+
However it is important to remember that since Java does not have operator overloading <em>if you want to use your</em> <c>equals</c> <em>method you must call it directly</em>.
581
584
Therefore once you write your own <c>equals</c> method:
@@ -616,6 +621,7 @@ public boolean equals(Fraction other) {
616
621
}
617
622
</code>
618
623
</program>
624
+
</listing>
619
625
620
626
<p>
621
627
One important thing to remember about <c>equals</c> is that it only checks to see if two objects are equal – it does not have any notion of less than or greater than.
@@ -634,26 +640,27 @@ public boolean equals(Fraction other) {
634
640
If you look at the documentation for <c>Integer</c> you will see that <c>Integer</c>’s parent class is <c>Number</c>.
635
641
Number is an <term>abstract class</term> that specifies several methods that all of its children must implement.
636
642
In Java an abstract class is more than just a placeholder for common methods.
637
-
In Java an abstract class has the power to specify certain methods that all of its children <term>must</term> implement.
643
+
In Java an abstract class has the power to specify certain methods that all of its children <em>must</em> implement.
638
644
You can trace this power back to the strong typing nature of Java.
639
645
</p>
640
646
641
647
<p>
642
-
Here is code that makes the <c>Fraction</c> class a child of <c>Number</c>:
648
+
<xrefref="java-class-extends"text="type-global"/> makes the <c>Fraction</c> class a child of <c>Number</c>.
643
649
</p>
644
650
645
-
646
-
<programxml:id="pjava-extends"language="java">
651
+
<listingxml:id="java-class-extends">
652
+
<program>
647
653
<code>
648
654
public class Fraction extends Number {
649
655
...
650
656
}
651
657
</code>
652
658
</program>
659
+
</listing>
653
660
654
661
<p>
655
-
<idx><c>extends</c></idx>
656
-
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.
662
+
<idx><c>extending a class</c></idx>
663
+
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.
657
664
A child class always extends its parent.
658
665
</p>
659
666
@@ -690,11 +697,11 @@ public class Fraction extends Number {
690
697
</p>
691
698
692
699
<p>
693
-
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:
700
+
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 <xrefref="java-object-type-conversions"text="type-global"/>.
However, and this is a big however, it is important to remember that if you specify <c>Number</c> as the type of a particular parameter then the Java compiler will <term>only let you use the methods of a</term> <c>Number</c>: <c>longValue</c>, <c>intValue</c>, <c>floatValue</c>, and <c>doubleValue</c>.
732
+
However, and this is a big however, it is important to remember that if you specify <c>Number</c> as the type of a particular parameter then the Java compiler will <em>only let you use the methods of a</em> <c>Number</c>: <c>longValue</c>, <c>intValue</c>, <c>floatValue</c>, and <c>doubleValue</c>.
725
733
</p>
726
734
727
735
<p>
728
-
Suppose you try to define a method as follows:
736
+
Suppose you try to define a method as <xrefref="java-ugly-add-method"text="type-global"/>.
The Java compiler would give an error because <c>add</c> is not a defined method of the <c>Number</c> class.
742
-
You will <term>still get this error</term> even if all your code that calls this <c>test</c> method passes two <c>Fractions</c> as parameters (remember that <c>Fraction</c> does implement <c>add</c>).
751
+
You will <em>still get this error</em> even if all your code that calls this <c>test</c> method passes two <c>Fractions</c> as parameters (remember that <c>Fraction</c> does implement <c>add</c>).
743
752
</p>
744
753
</subsection>
745
754
</section>
@@ -748,7 +757,6 @@ public void test(Number a, Number b) {
748
757
<title>Interfaces</title>
749
758
750
759
<p>
751
-
<idx><c>Comparable</c></idx>
752
760
<idx>single inheritance</idx>
753
761
Lets turn our attention to making a list of fractions sortable by the standard Java sorting method <c>Collections.sort</c>.
754
762
In Python, we would just need to implement the <c>__cmp__</c> method.
0 commit comments