16: Equals Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

Generics, Lists, Interfaces
Identity and Equality Based on material by Michael Ernst, University of Washington.
Equality Programming in C# Equality CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Comparable and Comparator Nuts and Bolts. 2 Nuts and bolts Four methods underlie many of Java’s important Collection types: equals, compare and compareTo,
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
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.
Exceptions. 2 Objectives Introduce C# exception handling –library exception types –custom exceptions Describe keywords used for exception handling –try.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Encapsulation CMSC 202. Types of Programmers Class programmers – Developers of new classes – Goal: Expose the minimum interface necessary to use a new.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Equality Programming in C# Equality CSE / ECE 668 Prof. Roger Crawfis.
Object Oriented Concepts
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
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.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
Types in programming languages1 What are types, and why do we need them?
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
Comparable and Comparator Nuts and Bolts. Sets A set is a collection in which all elements are unique—an element is either in the set, or it isn’t In.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Copyright © 2015 Curt Hill Java for Minecraft Those things you should know.
Symbol Tables From “Algorithms” (4 th Ed.) by R. Sedgewick and K. Wayne.
Operator Overloading Week 5.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
Session 06: C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. FEN AK - IT: Softwarekonstruktion.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Operators and Cast. Operators Operator Shortcuts.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Searching.
Computing with C# and the .NET Framework
Object-oriented Programming in Java
Classes and Inheritance
C# Operator Overloading and Type Conversions
Inheritance and Polymorphism
Future of C#
Delegates and Events 14: Delegates and Events
15: Object Object Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Collections 24: Collections Programming C# © 2003 DevelopMentor, Inc.
CS 302 Week 11 Jim Williams, PhD.
Non-static classes.
5.1 Being Objects and A Glimpse into Coding
Implementing Polymorphism
CS313D: Advanced Programming Language
Overloading and Constructors
Comparable and Comparator
Properties 7: Properties Programming C# © 2003 DevelopMentor, Inc.
CSE 1030: Implementing Non-Static Features
Building Java Programs
Comparable and Comparator
Generic Classes and Methods
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism Part 2
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
CS 240 – Advanced Programming Concepts
Presentation transcript:

16: Equals Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Objectives Describe equality testing 16: Equals Objectives Describe equality testing Show how to override Object Equals error checking casting chaining to base class comparing data Present static helper method Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Equality methods in Object class 16: Equals Equality methods in Object class Object class provides two methods to test equality instance version is primary method static version is convenient helper class Object { public virtual bool Equals(object o) ... public static bool Equals(object a, object b) ... ... } equality methods Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Object Equals Object class instance Equals compares references does not compare object data performs identity test Student a = new Student("Ann", 32, 8000); Student b = new Student("Ann", 32, 8000); if (a.Equals(b)) ... false, different objects Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Override Can override instance Equals virtual method typical motivation is when distinct objects might be equal class Student { public override bool Equals(object o) ... } ... } override Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Override details Several steps to override instance Equals test for null verify compared objects have same type chain to base class downcast parameter compare data Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Test for null Should test parameter against null return false if null 16: Equals Test for null Should test parameter against null return false if null not an error, do not throw exception class Student { public override bool Equals(object o) if (o == null) return false; ... } ... } null? Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Type testing Should verify objects have same type 16: Equals Type testing Should verify objects have same type typical to compare Type objects to require exactly same type not common to use is operator class Student { public override bool Equals(object o) ... if (this.GetType() != o.GetType()) return false; } ... } not same type? Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Downcast and compare Must downcast parameter to access data 16: Equals Downcast and compare Must downcast parameter to access data can choose to compare some or all data class Student { int id; public override bool Equals(object o) ... Student s = (Student)o; return this.id == s.id; } ... } downcast compare data Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Inheritance Equals typically chains to base version if provided allows base version to compare base data return false if base parts not equal class Person { string name; int age; public override bool Equals(object o) { ... } ... } base provides Equals method class Student : Person { public override bool Equals(object o) ... if (!base.Equals(o)) return false; } ... } derived version chains to base Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Do not chain to Object instance Equals Do not chain to Object version of Equals Object version performs identity test returns true only if comparing the same objects base class is Object class Person { public override bool Equals(object o) ... } ... } Equals should not chain to base Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Complete implementation 16: Equals Complete implementation Complete code for reference class Student : Person { public override bool Equals(object o) if (o == null) return false; if (this.GetType() != o.GetType()) return false; if (!base.Equals(o)) return false; Student s = (Student)o; return this.id == s.id; } ... } null? not same type? compare base downcast compare data Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Static Equals Object provides convenient static version of Equals main service is handling of null references returns false if first argument is null static Equals public static bool Equals(object a, object b) { if (a == b) return true; if (a == null || b == null) return false; return a.Equals(b); } true if same object or both are null delegate to instance Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

GetHashCode and Equals Equal objects must give same hash code if type overrides Equals it must also override GetHashCode Allows efficient containment test in hash tables can hash tested object and search only that bucket Student a = new Student("Ann", 32, 8000); Student b = new Student("Ann", 32, 8000); if (a.Equals(b)) ... if (a.GetHashCode() == b.GetHashCode()) if objects are equal hash codes must be the same Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Summary Object class provides framework for equality testing 16: Equals Summary Object class provides framework for equality testing instance Equals static Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003