skip to main content
10.1145/611892.611987acmconferencesArticle/Chapter ViewAbstractPublication PagessigcseConference Proceedingsconference-collections
Article

Implementing object equivalence in Java using the template method design pattern

Published:11 January 2003Publication History

ABSTRACT

A standard practice in object-oriented programming is to implement an operation, called equals in Java, for testing the equality of two objects. The equals method should be defined for every new Java class, but because of the intricacies of inheritance, casting, and dynamic typing, equals is often quite difficult to write correctly. And unfortunately many textbooks present flawed implementations of this operation. In this paper, we present a semantically correct technique for testing object equivalence, a technique that simultaneously brings together important mathematical foundations (equivalence relations), practical programming issues (inheritance, casting, dynamic typing), and sound software design (design patterns) in a natural and compelling way. While Java is used to demonstrate how the semantic flaws are corrected and the design improved using our techniques, the design is general enough that it will be clear how the same ideas could easily be extended to other languages such as C++.

References

  1. Bergin, J. Canonical Form of a Java Class. http://csis.pace.edu/~bergin/patterns/CanonicalJava.html.Google ScholarGoogle Scholar
  2. Bloch, J. Effective Java Programming Language Guide. Addison-Wesley (2001). Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Cohen, T. How Do I Correctly Implement the equals() Method? Dr. Dobb's Journal, May (2002).Google ScholarGoogle Scholar
  4. Gamma, E., Helm, R., Johnson, R., and Vlissides, J. Design Patterns: Elements of Reuseable Object-Oriented Software. Addison-Wesley Publishing (1995). Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Horstmann, C., and Cornell, G. Core Java 2: Volume I - Fundamentals. Sun Microsystems Press (2001). Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Stevenson, D.E., and Phillips, A.T. A Template Design for Correctly Overriding the Operator== in C++. UW-Eau Claire Computer Science Technical Report, August (2002).Google ScholarGoogle Scholar

Index Terms

  1. Implementing object equivalence in Java using the template method design pattern

    Recommendations

    Comments

    Login options

    Check if you have access through your login credentials or your institution to get full access on this article.

    Sign in
    • Published in

      cover image ACM Conferences
      SIGCSE '03: Proceedings of the 34th SIGCSE technical symposium on Computer science education
      February 2003
      444 pages
      ISBN:158113648X
      DOI:10.1145/611892

      Copyright © 2003 ACM

      Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 11 January 2003

      Permissions

      Request permissions about this article.

      Request Permissions

      Check for updates

      Qualifiers

      • Article

      Acceptance Rates

      Overall Acceptance Rate1,595of4,542submissions,35%

      Upcoming Conference

      SIGCSE Virtual 2024
      SIGCSE Virtual 2024: ACM Virtual Global Computing Education Conference
      November 30 - December 1, 2024
      Virtual Event , USA

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader