Object Class

As we discussed earlier in this chapter, all classes in Java inherit directly or indirectly from the Object class (package java.lang), so its 11 methods are inherited by all other classes. Figure 9.18 summarizes Object's methods.

Figure 9.18. Object methods that are inherited directly or indirectly by all classes.

Method

Description

clone

This protected method, which takes no arguments and returns an Object reference, makes a copy of the object on which it is called. When cloning is required for objects of a class, the class should override method clone as a public method and should implement interface Cloneable (package java.lang). The default implementation of this method performs a so-called shallow copyinstance variable values in one object are copied into another object of the same type. For reference types, only the references are copied. A typical overridden clone method's implementation would perform a deep copy that creates a new object for each reference type instance variable. There are many subtleties to overriding method clone. You can learn more about cloning in the following article:

java.sun.com/developer/JDCTechTips/2001/tt0306.html

equals

This method compares two objects for equality and returns TRue if they are equal and false otherwise. The method takes any Object as an argument. When objects of a particular class must be compared for equality, the class should override method equals to compare the contents of the two objects.
The method's implementation should meet the following requirements:

  • It should return false if the argument is null.
  • It should return true if an object is compared to itself, as in object1.equals( object1 ).
  • It should return true only if both object1.equals( object2 ) and object2.equals( object1 ) would return true.
  • For three objects, if object1.equals( object2 ) returns true and object2.equals( object3 ) returns true, then object1.equals( object3 ) should also return true.
  • If equals is called multiple times with the two objects and the objects do not change, the method should consistently return TRue if the objects are equal and false otherwise.

A class that overrides equals should also override hashCode to ensure that equal objects have identical hashcodes. The default equals implementation uses operator == to determine whether two references refer to the same object in memory. Section 29.3.3 demonstrates class String's equals method and differentiates between comparing String objects with == and with equals.

finalize

This protected method (introduced in Section 8.10 and Section 8.11) is called by the garbage collector to perform termination housekeeping on an object just before the garbage collector reclaims the object's memory. It is not guaranteed that the garbage collector will reclaim an object, so it cannot be guaranteed that the object's finalize method will execute. The method must specify an empty parameter list and must return void. The default implementation of this method serves as a placeholder that does nothing.

getClass

Every object in Java knows its own type at execution time. Method getClass (used in Section 10.5 and Section 21.3) returns an object of class Class (package java.lang) that contains information about the object's type, such as its class name (returned by Class method getName). You can learn more about class Class in the online API documentation at java.sun.com/j2se/5.0/docs/api/java/lang/Class.html.

hashCode

A hashtable is a data structure (discussed in Section 19.10) that relates one object, called the key, to another object, called the value. When initially inserting a value into a hashtable, the key's hashCode method is called. The hashcode value returned is used by the hashtable to determine the location at which to insert the corresponding value. The key's hashcode is also used by the hashtable to locate the key's corresponding value.

notify, notifyAll, wait

Methods notify, notifyAll and the three overloaded versions of wait are related to multithreading, which is discussed in Chapter 23. In J2SE 5.0, the multithreading model has changed substantially, but these features continue to be supported.

toString

This method (introduced in Section 9.4.1) returns a String representation of an object. The default implementation of this method returns the package name and class name of the object's class followed by a hexadecimal representation of the value returned by the object's hashCode method.

We discuss several of Object methods throughout this book (as indicated in the table). You can learn more about Object's methods in Object's online API documentation and in The Java Tutorial at the following sites:

java.sun.com/j2se/5.0/docs/api/java/lang/Object.html

java.sun.com/docs/books/tutorial/java/javaOO/objectclass

Recall from Chapter 7 that arrays are objects. As a result, like all other objects, an array inherits the members of class Object. Note that arrays have an overridden clone method that copies the array. However, if the array stores references to objects, the objects are not copied. For more information about the relationship between arrays and class Object, please see Java Language Specification, Chapter 10, at

java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net