diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 9ad5051..599f0d8 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -217,6 +217,7 @@ public void setDenominator(Integer denominator) { // setter method

constructor + Once you have identified the instance variables for your class the next thing to consider is the constructor. In Java, constructors have the same name as the class and are declared public. They are declared without a return type. @@ -281,8 +282,8 @@ public Fraction(Integer num, Integer den) {

  • pass-by-value - value of the reference - Java is strictly pass-by-value. For primitive types (like int), a copy of the value is passed. For object types (like our Fraction), a copy of the value of the reference (the memory address) is passed. + + Java is strictly pass-by-value. For primitive types (like int), a copy of the value is passed. For object types (like our Fraction), a copy of the reference(Namely, the memory address) is passed.

  • @@ -470,15 +471,16 @@ public class Fraction {

    - 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 .

    - - + + Fraction@6ff3c5b5 +

    The reason is that we have not yet provided a friendly string representation for our Fraction objects. @@ -495,7 +497,7 @@ Fraction@6ff3c5b5

    toString In Java, the equivalent of __str__ is the toString method. - Every object in Java already has a toString method defined for it because every class in Java automatically inherits from the Object class. + Every object in Java already has a toString method defined for it because every class in Java automatically inherits from the Object class. The Object class provides default implementations for the following methods.

    @@ -559,52 +561,55 @@ Fraction@6ff3c5b5

    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 toString method for the Fraction class. - A simple version of the method is provided below. + However, to make our output nicer we will implement the toString method for the Fraction class. shows a simple version of the method.

    - - + + public String toString() { return numerator.toString() + "/" + denominator.toString(); } +

    + equals The other important class for us to implement from the list of methods inherited from Object is the equals method. In Java, when two objects are compared using the == 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?). This is also the default behavior of the equals method provided by Object. The equals method allows us to decide if two objects are equal by looking at their instance variables. - However it is important to remember that since Java does not have operator overloading if you want to use your equals method you must call it directly. + However it is important to remember that since Java does not have operator overloading if you want to use your equals method you must call it directly. Therefore once you write your own equals method:

    - - + + object1 == object2 +

    - is NOT the same as: + is NOT the same as .

    - - + + object1.equals(object2) +

    - Here is an equals method for the Fraction class: + is an equals method for the Fraction class.

    - - + + public boolean equals(Fraction other) { Integer num1 = this.numerator * other.getDenominator(); @@ -616,6 +621,7 @@ public boolean equals(Fraction other) { } +

    One important thing to remember about equals 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) { If you look at the documentation for Integer you will see that Integer’s parent class is Number. Number is an abstract class that specifies several methods that all of its children must implement. In Java an abstract class is more than just a placeholder for common methods. - In Java an abstract class has the power to specify certain methods that all of its children must implement. + In Java an abstract class has the power to specify certain methods that all of its children must implement. You can trace this power back to the strong typing nature of Java.

    - Here is code that makes the Fraction class a child of Number: + makes the Fraction class a child of Number.

    - - + + public class Fraction extends Number { ... } +

    - extends - The keyword extends tells the compiler that the class Fraction extends, or adds new functionality to the Number class. + extending a class + The keyword extends tells the compiler that the class Fraction extends, or adds new functionality to the Number class. A child class always extends its parent.

    @@ -690,11 +697,11 @@ public class Fraction extends Number {

    - 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: + 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 .

    - - + + public double doubleValue() { return numerator.doubleValue() / denominator.doubleValue(); @@ -710,6 +717,7 @@ public long longValue() { } +

    is-a @@ -721,25 +729,26 @@ public long longValue() {

    - However, and this is a big however, it is important to remember that if you specify Number as the type of a particular parameter then the Java compiler will only let you use the methods of a Number: longValue, intValue, floatValue, and doubleValue. + However, and this is a big however, it is important to remember that if you specify Number as the type of a particular parameter then the Java compiler will only let you use the methods of a Number: longValue, intValue, floatValue, and doubleValue.

    - Suppose you try to define a method as follows: + Suppose you try to define a method as .

    - - + + public void test(Number a, Number b) { a.add(b); } +

    The Java compiler would give an error because add is not a defined method of the Number class. - You will still get this error even if all your code that calls this test method passes two Fractions as parameters (remember that Fraction does implement add). + You will still get this error even if all your code that calls this test method passes two Fractions as parameters (remember that Fraction does implement add).

    @@ -748,7 +757,6 @@ public void test(Number a, Number b) { Interfaces

    - Comparable single inheritance Lets turn our attention to making a list of fractions sortable by the standard Java sorting method Collections.sort. In Python, we would just need to implement the __cmp__ method.