Programming Languages and Techniques (CIS120e)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

The class Object. Lecture CS1122 Summer 2008

Programming Languages and Techniques (CIS120)

Expected properties of equality

CSE 331 Software Design & Implementation

equals() in the class Object

java.lang.object: Equality

Super-Classes and sub-classes

Programming Languages and Techniques (CIS120)

Methods Common to all Classes

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

CSE 331 Software Design & Implementation

Principles of Software Construction: Objects, Design and Concurrency. Inheritance, type-checking, and method dispatch. toad

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Equality. Michael Ernst. CSE 331 University of Washington

For this section, we will implement a class with only non-static features, that represents a rectangle

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Java Fundamentals (II)

Chapter 11: Collections and Maps

INSTRUCTIONS TO CANDIDATES

The Liskov Substitution Principle

Programming Languages and Techniques (CIS120)

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Domain-Driven Design Activity

COMP 250 Fall inheritance Nov. 17, 2017

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

Equality. Michael Ernst. CSE 331 University of Washington

Creating an Immutable Class. Based on slides by Prof. Burton Ma

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Programming Languages and Techniques (CIS120)

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

Abstract Classes and Interfaces

CS 251 Intermediate Programming Methods and Classes

/1,)0#(;"%0;)E#"),2+%B-) !"#$"%&&'($)*%($+%$,-)) %(.)/,01('2+,-) ,:) 6+FF#-,)R,),L;,(.)!#'(;)B'S,);1'-) T"#S,()6C&&,;"CU) *,0;+",)<=)

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Principles of Software Construction: Objects, Design and Concurrency. Polymorphism, part 2. toad Fall 2012

Practice for Chapter 11

Programming Languages and Techniques (CIS120)

Announcements. Equality. Lecture 10 Equality and Hashcode. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

CSE 373. Objects in Collections: Object; equals; compareto; mutability. slides created by Marty Stepp

CS 251 Intermediate Programming Methods and More

Programming Languages and Techniques (CIS120)

CS-202 Introduction to Object Oriented Programming

CS18000: Programming I

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Don t Believe the Hype. CS152: Programming Languages. Lecture 21 Object-Oriented Programming. Class-based OOP. So what is OOP?

Polymorphism. return a.doublevalue() + b.doublevalue();

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

Introduction to Object-Oriented Programming

INHERITANCE. Spring 2019

MET08-J. Preserve the equality contract when overriding the equals() method

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Java Object Oriented Design. CSC207 Fall 2014

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

C12a: The Object Superclass and Selected Methods

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

Derived and abstract data types. TDT4205 Lecture 15

The Java Type System (continued)

Example: Count of Points

G Programming Languages - Fall 2012

CS153: Compilers Lecture 11: Compiling Objects

CIS 120 Midterm II November 16, 2012 SOLUTIONS

CH. 2 OBJECT-ORIENTED PROGRAMMING

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Software Development (cs2500)

Object Oriented Programming. Java-Lecture 11 Polymorphism

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Inheritance (Part 5) Odds and ends

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Programming Languages and Techniques (CIS120)

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Programming Languages and Techniques (CIS120)

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

CSE 331 Final Exam 12/14/15 Sample Solution. Question 1. (20 points, 1 each) Warmup. For each statement, circle T if it is true and F if it is false.

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Lecture 16: HashTables 10:00 AM, Mar 2, 2018

Equality for Abstract Data Types

1 Shyam sir JAVA Notes

Transcription:

Programming Languages and Techniques (CIS120e) Lecture 33 Dec. 1, 2010 Equality

Consider this example public class Point {! private final int x; // see note about final*! private final int y;! public Point(int x, int y) {this.x = x; this.y = y; public int getx() { return x; public int gety() { return y; }! // somewhere in main! List<Point> l = new LinkedList<Point>();! l.add(new Point(1,2));! System.out.println(l.contains(new Point(1,2)));! What is printed to the terminal? Why? *Recall that the final modifier means that the field (or variable) cannot be modified once it is inipalized it is the closest analog in Java to OCaml s let- binding for names. CIS120e / Fall 2010 2

Equality* 1. IdenPty vs. Equality 2. PiWalls with implemenpng equals (with digressions) 3. Recipe for implemenpng equals *See the very nicely wriyen arpcle How to write an Equality Method in Java by Oderski, Spoon, and Venners (June 1, 2009!!) at hyp://www.arpma.com/lejava/arpcles/equality.html CIS120e / Fall 2010 3

IdenPty vs. Equality Object iden%ty is pointer equality Indicates where in the heap the object is located Tested using ==! Object equality is value, logical, or deep equality Indicates when two objects are the same as values Tested using the equals method inherited from Object! The default implementapon of equals is == In this case, instances are equal only to themselves Classes can override the default implementapon to provide a different logical nopon of equality. e.g. String s equals tests for idenpcal sequences of characters. CIS120e / Fall 2010 4

Logical Equality What does it mean for two things to be equal? It depends! In what way is the equality being used? Answer 1: Mutable objects are (usually) only equal to themselves Why? Answer 2: Two immutable objects (of the same type) are equal if their corresponding fields are equal What if there are unimportant fields? What if the objects are of different types? What is a reasonable definipon of equality? CIS120e / Fall 2010 5

The contract for equals The equals method implements an equivalence rela%on on non- null objects. It is reflexive: for any non- null reference value x, x.equals(x) should return true It is symmetric: for any non- null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true It is transi%ve: for any non- null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any non- null reference values x and y, mulpple invocapons of x.equals(y) consistently return true or consistently return false, provided no informapon used in equals comparisons on the object is modified For any non- null reference x, x.equals(null) should return false. CIS120e / Fall 2010 6

Overriding equals CIS120e / Fall 2010 7

First ayempt public class Point {! private final int x;! private final int y;! public Point(int x, int y) {this.x = x; this.y = y; public int getx() { return x; public int gety() { return y; public boolean equals(point p) {! return (this.getx() == p.getx() &&! this.gety() == that.gety());! }! // somewhere in main! List<Point> l = new LinkedList<Point>();! l.add(new Point(1,2));! System.out.println(l.contains(new Point(1,2)));! CIS120e / Fall 2010 8

Overloading: A digression How does an overloaded method invocapon get resolved? CIS120e / Fall 2010 9

equals was overloaded not overridden public class Point {!! // overloaded, not overridden! public boolean equals(point p) {! return (this.getx() == p.getx() &&! this.gety() == that.gety());! }! // Illustrate the problem without using Lists:! Point p1 = new Point(1,2);! Point p2 = new Point(1,2);! Object o = p2;! System.out.println(p1.equals(o)); // prints false!! System.out.println(p1.equals(p2)); // prints true!! The type of equals as declared in Object is: public boolean equals(object o)! The implementapon above takes a Point not an Object! CIS120e / Fall 2010 10

Overloaded method declarapon Overloaded methods share the same name. Overloaded methods can differ based on the number and/or types of their parameters public class OverloadExample {! public int m() {return 3; public int m(int x) { return x; public int m(integer i) { return i.intvalue() + 100; public int m(int x, int y){ return x + y; public int m(object o) { return 42; *This code uses overloading in poor style for the sake of example overloaded methods should all behave basically the same. Otherwise, the programmer will be confused when m does one thing when passed an int and something completely different when passed an Integer. CIS120e / Fall 2010 11

Overloaded method invocapon resolupon Which version of an overloaded method is invoked depends on the number and sta%c types of the arguments. OverloadExample oe = new OverloadExample();! System.out.println(oe.m()); // prints 3! System.out.println(oe.m(17)); // prints 17! System.out.println(oe.m(new Integer(120))); // prints 220! System.out.println(oe.m(3,4)); // prints 7! System.out.println(oe.m("hello")); // prints 42! Notes: This means methods cannot be overloaded based on return type. CIS120e / Fall 2010 12

Precise matching If more than one method s types match the invocapon, the most precise match is chosen: Type S is more precise than type T if S is a strict subtype of T. In our example there are two methods m that could be chosen for oe.m(new Integer(3)); int m(integer i) { } or int m(object o) { Precise matching says that the lem method is the one used, because Integer is more precise than Object. For methods with mulpple arguments, the most precise match is the one that has the greatest number of more precisely matching arguments.! Is there always such a method? CIS120e / Fall 2010 13

Ambiguous overloading public class OverloadExample {! public int m(integer i, Object o) { return i.intvalue() + 42; public int m(object o, Integer i) { return i.intvalue - 42;! OverloadExample oe = new OverloadExample();! System.out.println(oe.m(new Integer(3), hello ));! System.out.println(oe.m( hello, new Integer(3)));! System.out.println(oe.m(new Integer(3), new Integer(4)));! ambiguous invocapon CIS120e / Fall 2010 14

Overloading is useful: Overloading Summary to provide flexible handling of similar kinds of data (e.g. int vs. Integer) to provide default arguments (i.e. overloading the number of arguments) // Typical good use of overloading! public class Point {! private int x, y;! public Point() { this.x = 0; this.y = 0; }! public Point(int x, int y) { this.x = x; this.y = y;! Avoid ambiguous overloading! CIS120e / Fall 2010 15

Overriding equals, take two CIS120e / Fall 2010 16

Properly overridden equals public class Point {! @Override! public boolean equals(object o) {! // what do we do here???! }! Use the @Override annotapon when you intend to override a method so that the compiler can warn you about accidental overloading. Now what? How do we know whether the o is even a Point? We need a way to check the dynamic type of an object. CIS120e / Fall 2010 17

instanceof! Java provides the instanceof operator that tests the dynamic type of any object. Note: (null instanceof C) returns false for all C! Point p = new Point(1,2);! Object o1 = p;! Object o2 = hello ;! System.out.println(p instanceof Point); // prints true! System.out.println(o1 instanceof Point); // prints true! System.out.println(o2 instanceof Point); // prints false! System.out.println(p instanceof Object); // prints true! // Some instanceof tests are nonsensical:! System.out.println(p instanceof String); // compile-time error! In the case of equals, instanceof is appropriate because the method behavior depends on the dynamic types of two objects: o1.equals(o2) But use instanceof judiciously usually, dynamic dispatch is preferred. In fact, one could argue that overriding equals (and related mulpmethods ) is the only Pme one should use instanceof CIS120e / Fall 2010 18

Type Casts We can test whether o is a Point using instanceof public class Point {! @Override! Check whether o public boolean equals(object o) {! is a Point. boolean result = false;! if (o instanceof Point) {! // o is a point how do we treat it as such?!! return result;! }! Use a type cast: (Point) o At compile Pme: the expression (Point) o has type Point. At runpme: check whether the dynamic type of o is a subtype of Point, if so evaluate to o, otherwise raise a ClassCastException! As with instanceof, use casts judiciously i.e. almost never. Instead use generics CIS120e / Fall 2010 19

Refining the equals implementapon public class Point {! @Override! public boolean equals(object o) {! This cast is boolean result = false;! guaranteed to if (o instanceof Point) { succeed. Point that = (Point) o; result = (this.getx() == that.getx() &&! this.gety() == that.gety());!! return result;! }! Now the example code from the slide 2 will behave as expected. But are we done? Does this implementapon sapsfy the contract? CIS120e / Fall 2010 20

Equality and Subtypes CIS120e / Fall 2010 21

Suppose we extend Point like this public class ColoredPoint extends Point {! private final int color;! public ColoredPoint(int x, int y, int color) {! super(x,y);! this.color = color;! @Override public boolean equals(object o) {! boolean result = false;! if (o instanceof ColoredPoint) {! ColoredPoint that = (ColoredPoint) o;! result = (this.color == that.color &&! super.equals(that));! return result;! }! This version of equals is suitably modified to check the color field too. CIS120e / Fall 2010 22

Broken Symmetry Point p = new Point(1,2);! ColoredPoint cp = new ColoredPoint(1,2,17);! System.out.println(p.equals(cp)); // prints true! System.out.println(cp.equals(p)); // prints false! The problem arises because we mixed Points and ColoredPoints, and ColoredPoints have more data that allows for finer dispncpons. Should a Point ever be equal to a ColoredPoint? CIS120e / Fall 2010 23

Suppose Points can equal ColoredPoints public class ColoredPoint extends Point {! public boolean equals(object o) {! boolean result = false;! if (o instanceof ColoredPoint) {! ColoredPoint that = (ColoredPoint) o;! result = (this.color == that.color &&! super.equals(that));! } else if (o instanceof Point) {!! result = super.equals(that);! return result;! }! We can repair the symmetry violapon by checking for Point explicitly. Does this work? CIS120e / Fall 2010 24

Broken TransiPvity Point p = new Point(1,2);! ColoredPoint cp1 = new ColoredPoint(1,2,17);! ColoredPoint cp2 = new ColoredPoint(1,2,42);! System.out.println(p.equals(cp1)); // prints true! System.out.println(cp1.equals(p)); // prints true(!)! System.out.println(p.equals(cp2)); // prints true! System.out.println(cp1.equals(cp2)); // prints false(!!)! We fixed symmetry, but broke transipvity! Should a Point ever be equal to a ColoredPoint? No! CIS120e / Fall 2010 25

A Recipe for Equality* *Even this isn t the final story there is another version that uses reflecpon to check for class names. It doesn t work well with anonymous classes or subclasses that add only methods, but is simpler in other ways. See the Odersky arpcle for a discussion of the tradeoffs. CIS120e / Fall 2010 26

Add a canequal method. public class Point {! @Override public boolean equals(object o) {! boolean result = false;! if (o instanceof Point) { Point that = (Point) o; result = (that.canequal(this) &&! this.getx() == that.getx() &&! this.gety() == that.gety());!! return result;! }! public boolean canequal(object other) {! return (other instanceof Point);! Use the canequal method of the other object. Expose an instanceof test specialized to this parpcular class. CIS120e / Fall 2010 27

Override equals and canequal! public class ColoredPoint extends Point {!! @Override public boolean equals(object o) {! boolean result = false;! if (o instanceof ColoredPoint) {! ColoredPoint that = (ColoredPoint) o;! result = (that.canequal(this) &&! this.color == that.color &&! super.equals(that));! return result;! }! @Override public boolean canequal(object other) {! return (other instanceof ColoredPoint);! CIS120e / Fall 2010 28

The equals Recipe public class C extends D {!! @Override public boolean equals(object o) {!!if (this == o) return true;! boolean result = false;! if (o instanceof C) {! C that = (C) o;! result = (that.canequal(this) &&! this.field1 == that.field1 &&!! this.field2.equals(that.field2) &&! super.equals(that));! return result;! }! public boolean canequal(object other) {! return (other instanceof C);! 1. Override equals at the right type. 2. Return true in the case of idenpty. (This is an oppmizapon.) 3. Use instanceof and cast to check the other object s type. 4. Implement the canequal method and use the other object s version of it. 5. Compare all the corresponding fields, deferring to the superclass if needed. CIS120e / Fall 2010 29

Field Comparison When do you use == to compare fields? for fields that store primipve types (int, boolean, etc.) (omen) when the field is a reference to a mutable object when you want shallow equality (you don t want to follow pointers) When do you use equals to compare fields? for references to immutable value types (like String or Point) when you want to do deep equality (e.g. for singly- linked lists) my model: use equals to compare objects whose representapons in OCaml would not use the ref keyword (e.g. trees, etc.) Be careful about cycles! It s easy to cause equals to go into an infinite loop for cyclic (omen mutable) data structures. It s usually appropriate to defer the superclass to check its fields. But not in the first class to override equals! (Object uses pointer equality, remember!) Fields might be accessed directly or through accessor methods. CIS120e / Fall 2010 30

Some caveats Whenever you override equals you must also override canequal (assuming you follow the recipe given here) or provide it if the class is the first one in the inheritance tree to override equals Whenever you override equals you must also override hashcode in a compapble way hashcode is used by the HashSet and HashMap collecpons We ll talk about hashing more in the next lecture. CIS120e / Fall 2010 31

When to override equals In classes that represent (immutable) values String already overrides equals Our Point class is a good candidate When there is a logical nopon of equality The collecpons library overrides equality for Sets (e.g. two sets are equal if and only if they contain the same elements) Whenever instances of a class can serve as elements of a set or as keys in a map The collecpons library uses equals internally to define set membership and key lookup (This is the problem with the example code one slide 2.) CIS120e / Fall 2010 32

When not to override equals Each instance of a class is inherently unique O:en the case for mutable objects (since its state might change, the only sensible nopon of equality is idenpty) Classes that represent acpve enppes and not data (e.g. threads or gui components, etc.) A superclass already overrides equals and provides the correct funcponality. Usually the case when a subclass adds only new methods, not fields CIS120e / Fall 2010 33