Download presentation
Presentation is loading. Please wait.
1
Generic Classes and Methods
Chapter 5 Generic Classes and Methods © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
2
The Object Superclass The Object class defines a variety of methods, some of which have applications in operating systems and systems programming. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
3
Object Class Methods Boolean equals(Object obj) Indicates whether some other object is “equal to” this one. A class implements the method by first checking whether obj is an instance of the class. String toString() Returns a string representation of the object © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
4
Time24 Class Design A Time24 object has integer data variables, hour and minute, where the hour is in the range 0 to 23 and minute is in the range 0 to 59. The interface has methods to access and update the data members. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
5
Student Question How would you check equality of two times?
6
Time24 equals() public boolean equals(Object item){
// check the obj is a Time24 object if (item instanceof Time24){ // convert objects to minutes // and compare as integers Time24 t = (Time24)item; int time, ttime; time = hour * 60 + minute; ttime = t.hour * 60 + t.minute; // compare the two times return time == ttime; } // argument is not a Time24 object return false; © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
7
Generalized toString(arr)
// returns a string that represents // an array of objects; found in Arrays class public static String toString(Object[] arr){ if (arr == null) return "null"; else if (arr.length == 0) return "[]"; // start with the left bracket String str = "[" + arr[0]; // output all but the last element, // separating items with a comma. for (int i = 1; i < arr.length; i++) str += ", " + arr[i]; str += "]"; return str; } © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
8
Generalized Sequential Search
A generalized version of the sequential search specifies an array and a target of type Object. The array type must override the Object equals() method. The scan of the array uses the equals() method and polymorphism to locate the index of the target value. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
9
Sequential Search with int
public static int seqSearch(int[] arr, int first, int last, int target) { // scan elements in the range // first <= i < last; test for a match for (int i = first; i < last; i++) if (arr[i] == target) return i; // match not found return -1; } © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
10
Sequential Search with Object
public static int seqSearch(Object[] arr, int first, int last, Object target) { // scan elements in the range // first <= i < last; test for a match for (int i = first; i < last; i++) if (arr[i].equals(target)) return i; // match not found return -1; } © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
11
Design of a Canonical Class
Every class in Java is an Object; this means you may override some higher level methods
12
No Argument Constructor
Normal creation of classes The class and its constructors are known The new is used to create an instance Dynamic creation of classes at runtime In some applications, particularly when processing is distributed between machines, the class itself may not be known until runtime The JVM can still create instances of the class provided a no argument constructor is provided This ability to load classes and create instances at runtime is a very powerful feature in Java
13
Object Equality Conditions to be satisfied by an equals( ) method
public boolean equals(Object other) { if (this == other) return true; if (other instanceof C) { C otherObj = (C) other; -- compare each field and return false if not equal – return true; } else return false; }
14
Student Question Given a linked list implementation of a list structures, how would you override the equals method?
15
Equals for the Linked List
public boolean equals(Object other) { if (other != null && other instanceof LinkedList) { LinkedList otherlist = (LinkedList) other; if (this.size() == otherlist.size()) { Node thisnode = this.head; Node othernode = otherlist.head; while (thisnode!=null && othernode!=null) { if (!thisnode.item.equals(othernode.item)) return false; thisnode = thisnode.next; othernode = othernode.next; } return true; }} Equals for the Linked List
16
Overriding hashcode If equals is overriden, hashcode should be overriden A hashcode for two equal objects must return the same value; here is the hashcode for LinkedList
17
Please explain the following method
Student Question Please explain the following method
18
Calculating a Hashcode
Common ways to combine hash values
19
Cloning Objects
20
Student Question How would you clone a linked list?
21
Cloning Objects
22
Implementing toString
The toString method is defined for every object For complex objects, the default is to print the class name The programmer can override the toString method to make a more detailed printout of information How would you override toString for a linked list?
23
toString for LinkedList
public String toString() { StringBuffer s = new StringBuffer(); int i = 0; for (Node n = head; n != null; n = n.next, i++) { s.append("[" + i + "] = " + n.item + "\n"); } return s.toString();
24
Implementing Serialization
What is serialization? Transform an object to and from a byte stream Important if the object is to be sent over a network to a remote site or to be stored in a file If an object is to be serialized, the class should implement the java.io.Seralizable interface to custom define the methods writeObject( ) and readObject( ) Java provides a default implementation for objects This implementation is adequate for our LinkedList example
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.