Title: Exposure Java Slides
1Exposure Java-AB 2006
Chapter 27 Slides - Part 2
Focus on OOP Redefining Existing Methods
From Materials Created by Mr. Leon Schram
Originally created by Mr. John L. M. Schram
PowerPoint Presentation altered by Shannon
Pittman
2toString
3ArrayList and toString
ArrayList objects display the actual individual
array elements and not the memory reference of
the object. The output format looks like Tom,
Sue, Joe, Kathy. This means that the toString
method is redefined for the ArrayList class or
some superclass of ArrayList.
4// Java2710.java // This program demonstrates
"redefining the lttoStringgt method in the //
ltStudentgt class so that it displays every field
of a ltStudentgt object. // Note how this
implementation resembles the ltArrayListgt
format. import java.util.ArrayList public class
Java2710 public static void main (String
args) System.out.println("\nJava2710.java\n
") Student student1 new Student("Kathy
Alexander",21,3.475) Student student2 new
Student("Peter VanVliet",18,2.265) System.out.p
rintln(student1) System.out.println(student2)
System.out.println() class
Student private String name private int
age private double gpa public
Student(String n, int a, double g) name
n age a gpa g public String
toString() return "" name "," age
"," gpa ""
5Redefining toString
Every class has indirect access to the toString
method defined by the Object class. If some
output is desired with println that is different
from the default toString output, you must
redefine toString in the class that you are using
with a method, like the one shown below. public
String toString() String temp "Hours "
hours "\n" temp temp "Minutes "
minutes "\n" temp temp "Seconds "
seconds "\n" temp temp "Fractions "
fractions "\n" return temp
6equals
7Redefining equals
The operator compares the immediate values
stored by variables. This means that only
works correctly for primitive data types. Java
has an equals method, which works correctly for
comparing String objects.
8The equals Method
The equals method is originally defined in the
Object class. The default equals class does
little besides comparing actual values in
objects, which becomes a comparison of
references. Many classes redefine equals, such
as the String class and the ArrayList class.
Redefinition of the equals method normally
involves making a comparison of the values at the
referenced memory location.
9// Java2718.java This redefines the ltequalsgt
method for the ltStudentgt class. For this
implementation equality is based on equality in
every field. public class Java2718 public
static void main (String args)
System.out.println("\nJava2718.java\n")
Student student1 new Student("Meg",2.165) S
tudent student2 new Student("Tom",3.675) Stud
ent student3 new Student("Tom",3.675) if
(student1.equals(student2)) System.out.println(
"They are equal") else System.out.println("T
hey are not equal") System.out.println() if
(student2.equals(student3)) System.out.println(
"They are equal") else System.out.println("T
hey are not equal") System.out.println()
class Student private String name private
double gpa public Student(String n, double
g) name n gpa g public boolean
equals(Student source) return
(name.equals(source.name) gpa
source.gpa)
10compareTo
11// Java2719.java // This program demonstrates how
the ltcompareTogt // method work with ltStringgt
objects. public class Java2719 public static
void main (String args) System.out.println
("\nJava2719.java\n") String s1
"Aardvark" String s2 "Zulu" String s3
"Aardvark" System.out.println(s1 "
compares to " s2 " "
s1.compareTo(s2)) System.out.println(s2 "
compares to " s1 " "
s2.compareTo(s1)) System.out.println(s1 "
compares to " s3 " " s1.compareTo(s3))
System.out.println()
12// This is still program Java2722.java // This an
attempt to make the compareTo method simpler. //
This does not work. Source must be of type
Object. class Student implements
Comparable private String name private int
age public Student(String n, int a) name
n age a public int compareTo
(Student source) return name.compareTo(source
.name) public String toString() return
name
You might be wondering what point the comparable
interface is if it requires the method to be more
complicated. The purpose is that when you see
a class that implements a known interface, you
know what methods it will have, and how they are
expected to work.
13class Student implements Comparable private
String name private int age public
Student(String n, int a) name n age
a public int compareTo (Object
source) Student temp (Student)
source return name.compareTo(temp.name)
public String toString() return name
14Comparable and compareTo
Comparable is an interface with a single abstract
method, called compareTo. Access to the
compareTo method is only possible from a class,
which has implemented the compareTo method. Some
classes, like the String class have already
implemented the compareTo method. Implementing
is not redefining an inherited method. You are
defining the method for the first time.
15Parameter
Passing
A closer look...
16Java Parameter Passing
ALL parameters, in Java, are passed by value.
This means that a copy of the actual parameter
is assigned to the formal parameter. This is
true whether the actual parameter is a simple
data type or an object.
In the case of an object, a copy of the memory
address is sent. The reference is "passed by
value". In Java, you cannot "pass by reference".
17// Java2723.java // This program demonstrates
that the ltswapgt method does not alter // the
parameter values passed in the ltmaingt
method.
18// Java2724.java // This program demonstrates
that the ltswapgt method does not alter // the
ltStringgt values passed in the ltmaingt
method.
19// Java2725.java // This program demonstrates
that the ltswapgt method does not alter // the
ltStudentgt values passed in the ltmaingt
method.
20// Java2726.java // This program is the same as
Java2725 with the lttoStringgt redefinition removed
from // the ltStudentgt class, so that memory
references can be observed.
21What is going on? (Part 1)
Student st1 new Student("Sue",21,3.5) Student
st2 new Student("Tom",20,2.2)
22What is going on? (Part 2)
swap(st1,st2) public static void swap(Student
s1, Student s2)
23What is going on? (Part 3)
Student temp s1 s1 s2 s2 temp
24What is going on? (Part 4)
System.out.println("st1 " st1) System.out.prin
tln("st2 " st2)
25// Java2727.java // This program now appears to
work as expected. Note that this time the object
values, which are // references, are not swapped,
but the content values which are referenced by
the objects.
26// Java2728.java // This program uses a
ltcreateListgt method to generate random integers
for an // integer array. The program executes as
desired.
Output shown on next slide.
27(No Transcript)
28Making Changes with Method Calls
It is possible to call some method and make
changes to information that is stored by the
calling parameter variable. Just keep in mind
that you cannot change the direct reference value
of the calling parameter with a void
method. However, you can make changes to the
values that being referenced.
29// Java2729.java // This program is yet another
example that parameters are only passed by
value. // It is not possible to pass the
reference from the temp array to the list
parameter.
Output shown on next slide.
30(No Transcript)
31References and ParametersBottom Line
void methods cannot change the reference of the
calling parameter only the values being
referenced. return methods can change both the
values being referenced and the reference of the
calling parameter.
32// Java2730.java // If in fact it is desirable to
change the memory reference of an object, it can
be // done with a return method, as is
demonstrated in this program.
Output shown on next slide.
33(No Transcript)