Skip to content

Commit bd4970a

Browse files
authored
Merge pull request #87 from logananglin98/Issue#79Fix
Added Index Terms to Chapter 7
2 parents 9905a08 + a23e698 commit bd4970a

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

source/ch_7_definingclasses.ptx

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@
128128
</program>
129129

130130
<p>
131-
The instance variables (data members) we will need for our fraction class are the numerator and denominator. Of course in Python we can add instance variables to a class at any time by simply assigning a value to <c>objectReference.variableName</c>, whereas in Java all data members must be declared up front.
131+
<idx>data members</idx>
132+
The instance variables (<term>data members</term>) we will need for our fraction class are the numerator and denominator. Of course in Python we can add instance variables to a class at any time by simply assigning a value to <c>objectReference.variableName</c>, whereas in Java all data members must be declared up front.
132133
</p>
133134

134135
<p>
@@ -146,7 +147,7 @@
146147
</program>
147148

148149
<p>
149-
Notice that we have declared the numerator and denominator to be private.
150+
Notice that we have declared the numerator and denominator to be <term>private</term>.
150151
This means that the compiler will generate an error if another method tries to write code like the following:
151152
</p>
152153

@@ -159,9 +160,11 @@
159160
</program>
160161

161162
<p>
162-
Direct access to instance variables is not allowed.
163-
Therefore if we legitimately want to be able to access information such as the numerator or denominator for a particular fraction we must have getter methods.
164-
It is very common programming practice to provide getter and setter methods for instance variables in Java.
163+
<idx>getter method</idx>
164+
<idx>setter method</idx>
165+
Direct access to instance variables is not allowed in Java.
166+
Therefore if we legitimately want to be able to access information such as the numerator or the denominator for a particular fraction we must have a <term>getter method</term> that returns the needed value.
167+
Hence, it is a very common programming practice to both provide <term>getter methods</term> and <term>setter methods</term> when needed for instance variables in Java.
165168
</p>
166169

167170

@@ -188,8 +191,9 @@ public void setDenominator(Integer denominator) {
188191
<title>Writing a constructor</title>
189192

190193
<p>
194+
<idx>constructors</idx>
191195
Once you have identified the instance variables for your class the next thing to consider is the constructor.
192-
In Java, constructors have the same name as the class and are declared public.
196+
In Java, <term>constructors</term> have the same name as the class and are declared public.
193197
They are declared without a return type.
194198
So any method that is named the same as the class and has no return type is a constructor.
195199
Our constructor will take two parameters: the numerator and the denominator.
@@ -206,6 +210,7 @@ public Fraction(Integer top, Integer bottom) {
206210
</program>
207211

208212
<p>
213+
<idx><c>this</c></idx>
209214
There are a couple of important things to notice here.
210215
First, you will notice that the constructor does not have a <c>self</c> parameter.
211216
You will also notice that we can simply refer to the instance variables by name without the <c>self</c> prefix, because they have already been declared.
@@ -233,7 +238,7 @@ public Fraction(Integer num, Integer den) {
233238
<p>
234239
Now we come to one of the major differences between Java and Python.
235240
The Python class definition used the special methods for addition and comparison that have the effect of redefining how the standard operators behave: in Python, <c>__add__</c> and <c>__lt__</c> change the behavior of <c>+</c> and <c>&lt;</c>, respectively.
236-
In Java there is <term>no operator overloading</term>.
241+
In Java there is no operator overloading.
237242
So we will have to write the method for addition a little differently.
238243
</p>
239244

@@ -248,11 +253,14 @@ public Fraction(Integer num, Integer den) {
248253
<ul>
249254
<li>
250255
<p>
251-
<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>value of the reference</em> (the memory address) is passed.
256+
<idx>pass-by-value</idx>
257+
<idx>value of the reference</idx>
258+
<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.
252259
</p>
253260
</li>
254261
<li>
255262
<p>
263+
<idx>pass-by-assignment</idx>
256264
<term>Python is pass-by-assignment</term> (or pass-by-object-reference). Since everything in Python is an object, the rule is consistent: a copy of the reference to the object is passed.
257265
</p>
258266
</li>
@@ -331,16 +339,18 @@ public Fraction add(Fraction otherFrac) {
331339
</p>
332340

333341
<p>
342+
<idx>method overloading</idx>
334343
In Java we can do runtime type checking, but the compiler will not allow us to pass an Integer to the <c>add</c> method since the parameter has been declared to be a Fraction.
335344
The way that we solve this problem is by writing another <c>add</c> method with a different set of parameters.
336345
In Java this practice is legal and common we call this practice <term>method overloading</term>.
337346
</p>
338347

339348
<p>
349+
<idx>signature</idx>
340350
This idea of method overloading raises a very important difference between Python and Java.
341351
In Python a method is known by its name only.
342352
In Java a method is known by its signature.
343-
The signature of a method includes its name, and the types of all of its parameters.
353+
The <term>signature</term> of a method includes its name, and the types of all of its parameters.
344354
The name and the types of the parameters are enough information for the Java compiler to decide which method to call at runtime.
345355
</p>
346356

@@ -488,6 +498,8 @@ Fraction@6ff3c5b5
488498
<title>The <c>Object</c> Class</title>
489499

490500
<p>
501+
<idx>object class</idx>
502+
<idx><c>toString</c></idx>
491503
In Java, the equivalent of <c>__str__</c> is the <c>toString</c> method.
492504
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.
493505
The <c>Object</c> class provides default implementations for the following methods.
@@ -622,6 +634,7 @@ public boolean equals(Fraction other) {
622634
<title>Abstract Classes and Methods</title>
623635

624636
<p>
637+
<idx>abstract class</idx>
625638
If we want to make our <c>Fraction</c> class behave like <c>Integer</c>, <c>Double</c>, and the other numeric classes in Java then we need to make a couple of additional modifications to the class.
626639
The first thing we will do is plug <c>Fraction</c> into the Java class hierarchy at the same place as <c>Integer</c> and its siblings.
627640
If you look at the documentation for <c>Integer</c> you will see that <c>Integer</c>&#x2019;s parent class is <c>Number</c>.
@@ -645,6 +658,7 @@ public class Fraction extends Number {
645658
</program>
646659

647660
<p>
661+
<idx><c>extends</c></idx>
648662
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.
649663
A child class always extends its parent.
650664
</p>
@@ -704,9 +718,10 @@ public long longValue() {
704718
</program>
705719

706720
<p>
721+
<idx>is-a</idx>
707722
By having the <c>Fraction</c> class extend the <c>Number</c> class we can now pass a <c>Fraction</c> to any Java method that specifies it can receive a <c>Number</c> as one of its parameters.
708723
For example many Java user interface methods accept any object that is a subclass of <c>Number</c> as a parameter.
709-
In Java the class hierarchy and the &#x201C;is-a&#x201D; relationships are very important.
724+
In Java the class hierarchy and the &#x201C;<term>is-a</term>&#x201D; relationships are very important.
710725
Whereas in Python you can pass any kind of object as a parameter to any method or function, the strong typing of Java makes sure that you only pass an object as a parameter that is of the type specified in the method signature, or one of the children of the type specified.
711726
When you see a parameter of type <c>Number</c> it&#x2019;s important to remember that an <c>Integer</c> <em>is-a</em> <c>Number</c> and a <c>Double</c> <em>is-a</em> <c>Number</c> and a <c>Fraction</c> <em>is-a</em> <c>Number</c>, because these classes are children of <c>Number</c>.
712727
</p>
@@ -739,18 +754,21 @@ public void test(Number a, Number b) {
739754
<title>Interfaces</title>
740755

741756
<p>
757+
<idx><c>Comparable</c></idx>
758+
<idx>single inheritance</idx>
742759
Lets turn our attention to making a list of fractions sortable by the standard Java sorting method <c>Collections.sort</c>.
743760
In Python, we would just need to implement the <c>__cmp__</c> method.
744761
But in Java we cannot be that informal.
745762
In Java, things that are sortable must be <c>Comparable</c>.
746763
Your first thought might be that <c>Comparable</c> is superclass of <c>Number</c>, but that is actually not the case.
747-
Java only supports single inheritance, that is, a class can have only one parent.
764+
Java only supports <term>single inheritance</term>, that is, a class can have only one parent.
748765
Although it would be possible to add an additional layer to the class hierarchy it would also complicate things dramatically, because not only are <c>Numbers</c> comparable, but <c>Strings</c> are also <c>Comparable</c> as would many other types.
749766
For example, we might have a <c>Student</c> class and we want to be able to sort students by their GPA.
750767
But <c>Student</c> might already extends the class <c>Person</c> for which there would be no natural comparison method.
751768
</p>
752769

753770
<p>
771+
<idx><c>Interface</c></idx>
754772
Java&#x2019;s answer to this problem is the <c>Interface</c> mechanism.
755773
Interfaces are like a combination of &#x201C;inheritance&#x201D; and &#x201C;contracts&#x201D; all rolled into one.
756774
An interface is a <em>specification</em> that says any object that claims it implements this interface must provide the following methods.
@@ -863,7 +881,8 @@ public class Student {
863881
</program>
864882

865883
<p>
866-
In this example notice that we create a static member variable by using the <c>static</c> modifier on the variable declaration. Once a variable has been declared <c>static</c> in Java it can be accessed from inside the class without prefixing the name of the class as we had to do in Python.
884+
<idx>static member variable</idx>
885+
In this example notice that we create a <term>static member variable</term> by using the <c>static</c> modifier on the variable declaration. Once a variable has been declared <c>static</c> in Java it can be accessed from inside the class without prefixing the name of the class as we had to do in Python.
867886
</p>
868887
</section>
869888

0 commit comments

Comments
 (0)