Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120e)

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)

Programming Languages and Techniques (CIS120)

The Liskov Substitution Principle

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

equals() in the class Object

Programming Languages and Techniques (CIS120)

Methods Common to all Classes

Programming Languages and Techniques (CIS120)

Chair of Software Engineering Java and C# in Depth

java.lang.object: Equality

Super-Classes and sub-classes

Inheritance and delega9on

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Java Fundamentals (II)

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

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

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

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

Domain-Driven Design Activity

Programming Languages and Techniques (CIS120)

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Expected properties of equality

CSE 331 Software Design & Implementation

Programming Languages and Techniques (CIS120)

Charlie Garrod Michael Hilton

Practice for Chapter 11

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

Example: Count of Points

CSE 331 Software Design & Implementation

Java Object Oriented Design. CSC207 Fall 2014

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

CIS 120 Midterm II November 16, 2012 SOLUTIONS

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

Principles of So3ware Construc9on: Objects, Design, and Concurrency. Introduc9on to Java. Josh Bloch Charlie Garrod Darya Melicher

Chapter 11: Collections and Maps

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

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit

Generic programming POLYMORPHISM 10/25/13

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

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

Encapsula)on, cont d. Polymorphism, Inheritance part 1. COMP 401, Spring 2015 Lecture 7 1/29/2015

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

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

Uguaglianza e Identità. (no, non avete sbagliato corso )

CSE 143 Lecture 22. The Object class; Polymorphism. read slides created by Marty Stepp and Ethan Apter

Abstract Classes and Interfaces

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Why OO programming? want but aren t. Ø What are its components?

index.pdf January 21,

CS-202 Introduction to Object Oriented Programming

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

What is Inheritance?

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

Object-Oriented Programming in the Java language

Objects and Classes (1)

COMP 250 Fall inheritance Nov. 17, 2017

ITI Introduction to Computing II

ITI Introduction to Computing II

Structure Patters: Bridge and Flyweight. CSCI 3132 Summer

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

CLASS DESIGN. Objectives MODULE 4

Introduction to Programming Using Java (98-388)

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Programming Languages and Techniques (CIS120)

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

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.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Equality. Michael Ernst. CSE 331 University of Washington

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

Equality. Michael Ernst. CSE 331 University of Washington

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

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

Inheritance (Part 5) Odds and ends

1B1b. Classes in Java Part III. Review. Static. Static (2) Example. Static (3) 1B1b Lecture Slides. Copyright 2004, Graham Roberts 1

ITI Introduction to Computing II

Garbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; }

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

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

Java and C# in Depth

More about inheritance

Transcription:

Programming Languages and Techniques (CIS120) Lecture 36 April 23, 2014 Overriding and Equality

HW 10 has a HARD deadline Announcements You must submit by midnight, April 30 th Demo your project to your TA during reading days Friday s lecture is a BONUS lecture Not directly related to game project or Java libraries No clicker quizzes Fun! Senior project demos this morning. Check them out axer class! CIS120

Clicker quiz 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; // 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 gets printed to the console? 1. true 2. false

Method Overriding

A Subclass can Override its Parent public class C {! public void printname() { System.out.println( I m a C );!! public class D extends C {! public void printname() { System.out.println( I m a D );!! C c = new D();! c.printname(); // what gets printed?! What gets printed to the console? 1. I m a C 2. I m a D 3. NullPointerExcep]on 4. NoSuchMethodExcep]on

A Subclass can Override its Parent public class C {! public void printname() { System.out.println( I m a C );!! public class D extends C {! public void printname() { System.out.println( I m a D );!! C c = new D();! c.printname(); // what gets printed?! Our ASM model for dynamic dispatch already explains what will happen when we run this code. Useful for changing the default behavior of classes. But can be confusing and difficult to reason about if not used carefully.

Overriding Example Workspace Stack Heap C c = new D();! c.printname();! Class Table Object! String tostring(){ boolean equals!! C! extends! C() {! void printname(){! D! extends! D() {! void printname(){!

Overriding Example Workspace Stack Heap Class Table c.printname();! c! D! Object! String tostring(){ boolean equals!! C! extends! C() {! void printname(){! D! extends! D() {! void printname(){!

Overriding Example Workspace Stack Heap Class Table.printName();! c! D! Object! String tostring(){ boolean equals!! C! extends! C() {! void printname(){! D! extends! D() {! void printname(){!

Overriding Example Workspace Stack Heap Class Table System.out. println( I m a D );! c! D! Object! String tostring(){ this! boolean equals!! C! extends! C() {! void printname(){! D! extends! D() {! void printname(){!

Difficulty with Overriding class C { public void printname() { System.out.println("I'm a " + getname()); public String getname() { return "C"; class E extends C { public String getname() { return "E"; What gets printed to the console? 1. I m a C 2. I m a E 3. I m an E 4. NullPointerExcep]on // in main C c = new E(); c.printname();!

class C { Difficulty with Overriding public void printname() { System.out.println("I'm a " + getname()); public String getname() { return "C"; class E extends C { public String getname() { return "E"; // in main C c = new E(); c.printname();! The C class might be in another package, or a library... Whoever wrote D might not be aware of the implica]ons of changing getname. Overriding What gets the printed method to the causes console? the behavior of printname to change! 1. I m a C 2. I m a E Overriding can break invariants/ 3. I m an E abstrac]ons relied upon by the 4. NullPointerExcep]on superclass.

When To Override? Only override methods when the parent class is designed specifically to support such modifica]ons: If the library designer specifically describes the behavioral contract that the parent methods assume about overridden methods (e.g. equals, paintcomponent) If you re wri]ng the code for both the parent and child class (and will maintain control of both parts as the soxware evolves) it might be OK to overrride. Either way: document the design Use the @Override annota]on to mark inten]onal overriding Look for other means of achieving the desired outcome: Use composi]on & delega]on (i.e. wrapper objects) rather than overriding

How to prevent overriding By default, methods can be overridden in subclasses. The final modifier changes that. Final methods cannot be overridden in subclasses Prevents subclasses from changing the behavioral contract between methods by overriding static final methods cannot be hidden Similar, but not the same as final fields and local variables: Act like the immutable name bindings in OCaml Must be ini]alized (either by a sta]c ini]alizer or in the constructor) and cannot thereaxer be modified. static final fields are useful for defining constants (e.g. Math.PI)

Case study: Equality

Mo]va]ng example 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; // somewhere in main List<Point> l = new LinkedList<Point>(); l.add(new Point(1,2)); System.out.println(l.contains(new Point(1,2)));! We must override equals in the Point class to get the desired behavior.

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 no]on of equality The collec]ons library overrides equality for Sets (e.g. two sets are equal if and only if they contain equal elements) Whenever instances of a class might need to serve as elements of a set or as keys in a map The collec]ons library uses equals internally to define set membership and key lookup (This is the problem with the example code)

When not to override equals When each instance of a class is inherently unique O6en the case for mutable objects (since its state might change, the only sensible no]on of equality is iden]ty) Classes that represent ac]ve en]]es rather than data (e.g. threads, gui components, etc.) When a superclass already overrides equals and provides the correct func]onality. Usually the case when a subclass adds only new methods, not fields

How to override equals *See the very nicely wriien ar]cle How to write an Equality Method in Java by Oderski, Spoon, and Venners (June 1, 2009) at hip://www.ar]ma.com/lejava/ar]cles/equality.html

The contract for equals The equals method implements an equivalence rela8on 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 transi8ve: 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, mul]ple invoca]ons of x.equals(y) consistently return true or consistently return false, provided no informa]on used in equals comparisons on the object is modified For any non- null reference x, x.equals(null) should return false. Directly from: hip://docs.oracle.com/javase/6/docs/api/java/lang/object.html#equals(java.lang.object)

First aiempt 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 that) { return (this.getx() == that.getx() && this.gety() == that.gety());!

Gocha: overloading, vs. overriding public class Point { // overloaded, not overridden public boolean equals(point that) { return (this.getx() == that.getx() && this.gety() == that.gety());! 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 implementa]on above takes a Point not an Object!

Overriding equals, take two

Properly overridden equals public class Point { @Override public boolean equals(object o) { if (o == null) { return false; // what do we do here???! Start with the null check. Why can we immediately return false? Use the @Override annota]on 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.

instanceof! The instanceof operator tests the dynamic type of any object 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 System.out.println(null instanceof Object); // prints false What gets printed? (1=true, 2=false) 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 beier.

Type Casts We can test whether o is a Point using instanceof @Override public boolean equals(object o) { if (o == null) { return false; if (!(o instanceof Point)) { return false; // o is a point - how do we treat it as such?! Use a type cast: (Point) o At compile ]me: the expression (Point) o has type Point. At run]me: 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

Refining the equals implementa]on @Override public boolean equals(object o) { if (o == null) { return false; if (!(o instanceof Point)) { return false; Point that = (Point) o; if (x!= that.x) { return false; if (y!= that.y) { return false; return true;! This cast is guaranteed to succeed.

One more addi]on An op]miza]on for @Override when the argument public boolean equals(object o) { is an alias. if (this == o) { return true; if (o == null) { return false; if (!(o instanceof Point)) { return false; Point that = (Point) o; if (x!= that.x) { return false; if (y!= that.y) { return false; return true;! Now the example code from the slide 3 will behave as expected. But are we done? Does this implementa]on sa]sfy the contract?