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
Copy file name to clipboardExpand all lines: source/ch_7_definingclasses.ptx
+31-12Lines changed: 31 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -128,7 +128,8 @@
128
128
</program>
129
129
130
130
<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.
132
133
</p>
133
134
134
135
<p>
@@ -146,7 +147,7 @@
146
147
</program>
147
148
148
149
<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>.
150
151
This means that the compiler will generate an error if another method tries to write code like the following:
151
152
</p>
152
153
@@ -159,9 +160,11 @@
159
160
</program>
160
161
161
162
<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.
165
168
</p>
166
169
167
170
@@ -188,8 +191,9 @@ public void setDenominator(Integer denominator) {
188
191
<title>Writing a constructor</title>
189
192
190
193
<p>
194
+
<idx>constructors</idx>
191
195
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.
193
197
They are declared without a return type.
194
198
So any method that is named the same as the class and has no return type is a constructor.
195
199
Our constructor will take two parameters: the numerator and the denominator.
@@ -206,6 +210,7 @@ public Fraction(Integer top, Integer bottom) {
206
210
</program>
207
211
208
212
<p>
213
+
<idx><c>this</c></idx>
209
214
There are a couple of important things to notice here.
210
215
First, you will notice that the constructor does not have a <c>self</c> parameter.
211
216
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) {
233
238
<p>
234
239
Now we come to one of the major differences between Java and Python.
235
240
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><</c>, respectively.
236
-
In Java there is <term>no operator overloading</term>.
241
+
In Java there is no operator overloading.
237
242
So we will have to write the method for addition a little differently.
238
243
</p>
239
244
@@ -248,11 +253,14 @@ public Fraction(Integer num, Integer den) {
248
253
<ul>
249
254
<li>
250
255
<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.
252
259
</p>
253
260
</li>
254
261
<li>
255
262
<p>
263
+
<idx>pass-by-assignment</idx>
256
264
<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.
257
265
</p>
258
266
</li>
@@ -331,16 +339,18 @@ public Fraction add(Fraction otherFrac) {
331
339
</p>
332
340
333
341
<p>
342
+
<idx>method overloading</idx>
334
343
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.
335
344
The way that we solve this problem is by writing another <c>add</c> method with a different set of parameters.
336
345
In Java this practice is legal and common we call this practice <term>method overloading</term>.
337
346
</p>
338
347
339
348
<p>
349
+
<idx>signature</idx>
340
350
This idea of method overloading raises a very important difference between Python and Java.
341
351
In Python a method is known by its name only.
342
352
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.
344
354
The name and the types of the parameters are enough information for the Java compiler to decide which method to call at runtime.
345
355
</p>
346
356
@@ -488,6 +498,8 @@ Fraction@6ff3c5b5
488
498
<title>The <c>Object</c> Class</title>
489
499
490
500
<p>
501
+
<idx>object class</idx>
502
+
<idx><c>toString</c></idx>
491
503
In Java, the equivalent of <c>__str__</c> is the <c>toString</c> method.
492
504
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.
493
505
The <c>Object</c> class provides default implementations for the following methods.
@@ -622,6 +634,7 @@ public boolean equals(Fraction other) {
622
634
<title>Abstract Classes and Methods</title>
623
635
624
636
<p>
637
+
<idx>abstract class</idx>
625
638
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.
626
639
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.
627
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>.
@@ -645,6 +658,7 @@ public class Fraction extends Number {
645
658
</program>
646
659
647
660
<p>
661
+
<idx><c>extends</c></idx>
648
662
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.
649
663
A child class always extends its parent.
650
664
</p>
@@ -704,9 +718,10 @@ public long longValue() {
704
718
</program>
705
719
706
720
<p>
721
+
<idx>is-a</idx>
707
722
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.
708
723
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 “is-a” relationships are very important.
724
+
In Java the class hierarchy and the “<term>is-a</term>” relationships are very important.
710
725
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.
711
726
When you see a parameter of type <c>Number</c> it’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>.
712
727
</p>
@@ -739,18 +754,21 @@ public void test(Number a, Number b) {
739
754
<title>Interfaces</title>
740
755
741
756
<p>
757
+
<idx><c>Comparable</c></idx>
758
+
<idx>single inheritance</idx>
742
759
Lets turn our attention to making a list of fractions sortable by the standard Java sorting method <c>Collections.sort</c>.
743
760
In Python, we would just need to implement the <c>__cmp__</c> method.
744
761
But in Java we cannot be that informal.
745
762
In Java, things that are sortable must be <c>Comparable</c>.
746
763
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.
748
765
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.
749
766
For example, we might have a <c>Student</c> class and we want to be able to sort students by their GPA.
750
767
But <c>Student</c> might already extends the class <c>Person</c> for which there would be no natural comparison method.
751
768
</p>
752
769
753
770
<p>
771
+
<idx><c>Interface</c></idx>
754
772
Java’s answer to this problem is the <c>Interface</c> mechanism.
755
773
Interfaces are like a combination of “inheritance” and “contracts” all rolled into one.
756
774
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 {
863
881
</program>
864
882
865
883
<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.
0 commit comments