Generic Classes and Methods

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Strings in Java 1. strings in java are handled by two classes String &
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 11 Implementing.
1 Class Design CS 3331 Fall Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode method.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
More about inheritance Exploring polymorphism 5.0.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
Object-oriented Programming in Java. What is OOP?  The goal is (subtype) polymorphism  Achieved by Classes (user-defined types) Classes (user-defined.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 8 Collection.
ADSA: Generics/ Advanced Data Structures and Algorithms Objective –to describe basic forms of generic classes, interfaces, and methods for searching.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
More about inheritance Exploring polymorphism 5.0.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 COS 260 DAY 19 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz not corrected yet 9 Th Mini Quiz next class –Due November 19 Finish Discussion on More.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Object-Oriented Concepts
From Building Blocks to Projects
Object-oriented Programming in Java
Objects First with Java Creating cooperating objects
Software Development Java Classes and Methods
CSC 205 Java Programming II
More about inheritance
Lecture 17: Polymorphism (Part II)
CSE 413, Autumn 2002 Programming Languages
Chapter 8 Classes and Objects
COS 260 DAY 19 Tony Gauvin.
Primitive Types Vs. Reference Types, Strings, Enumerations
Continuing Chapter 11 Inheritance and Polymorphism
5.1 Being Objects and A Glimpse into Coding
Recitation 6 Inheritance.
Overloading and Constructors
Data Structures for Java William H. Ford William R. Topp
COS 260 DAY 19 Tony Gauvin.
The Language Package.
Introduction to LINQ Chapter 11.
Building Java Programs
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Podcast Ch18b Title: STree Class
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Chapter 6 Control Statements: Part 2
Computer Science and Engineering
Introduction to Data Structure
Lecture 18: Polymorphism (Part II)
Recursive Objects Singly Linked Lists.
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Podcast Ch21a Title: Hash Functions
TCSS 143, Autumn 2004 Lecture Notes
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Review for Midterm 3.
Presentation transcript:

Generic Classes and Methods Chapter 5 Generic Classes and Methods © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

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.

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.

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.

Student Question How would you check equality of two times?

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.

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.

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.

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.

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.

Design of a Canonical Class Every class in Java is an Object; this means you may override some higher level methods

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

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; }

Student Question Given a linked list implementation of a list structures, how would you override the equals method?

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

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

Please explain the following method Student Question Please explain the following method

Calculating a Hashcode Common ways to combine hash values

Cloning Objects

Student Question How would you clone a linked list?

Cloning Objects

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?

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();

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