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

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

Principles of Software Construction: Objects, Design, and Concurrency

Inheritance and delega9on

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

Principles of Software Construction: Objects, Design, and Concurrency. Part 1: Designing Classes. Design for Reuse School of Computer Science

Principles of Software Construction: Objects, Design, and Concurrency (Part 1: Designing Classes) Design for Reuse (class level)

Class-level reuse with inheritance Behavioral subtyping

Josh Bloch Charlie Garrod Darya Melicher

The class Object. Lecture CS1122 Summer 2008

Intro to Computer Science 2. Inheritance

Methods Common to all Classes

Introduction to Inheritance

equals() in the class Object

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13

Inheritance (P1 2006/2007)

Super-Classes and sub-classes

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy

Programming Languages and Techniques (CIS120)

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

Josh Bloch Charlie Garrod Darya Melicher

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

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

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

Java Object Oriented Design. CSC207 Fall 2014

Chapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

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

Practice for Chapter 11

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

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

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

CSE 331 Software Design & Implementation

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

CS-202 Introduction to Object Oriented Programming

More Relationships Between Classes

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

Expected properties of equality

Java Fundamentals (II)

CSE 331 Software Design & Implementation

First IS-A Relationship: Inheritance

Equality. Michael Ernst. CSE 331 University of Washington

Object-Oriented Programming More Inheritance

Principles of Software Construction: Objects, Design and Concurrency. Mutability and Java Potpourri. toad Fall 2013

Programming Languages and Techniques (CIS120)

Object Oriented Programming. Java-Lecture 11 Polymorphism

CLASS DESIGN. Objectives MODULE 4

CMSC 132: Object-Oriented Programming II

The Liskov Substitution Principle

Programming Languages and Techniques (CIS120)

COMP 250 Fall inheritance Nov. 17, 2017

Location: Planet Laser Interview Skills Workshop

CHAPTER 10 INHERITANCE

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

Making New instances of Classes

Handout 9 OO Inheritance.

CS1083 Week 3: Polymorphism

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

java.lang.object: Equality

Check out Polymorphism from SVN. Object & Polymorphism

Equality. Michael Ernst. CSE 331 University of Washington

Charlie Garrod Michael Hilton

Inheritance and Subclasses

Rules and syntax for inheritance. The boring stuff

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

CS/ENGRD 2110 SPRING 2018

Programming Languages and Techniques (CIS120e)

JAVA MOCK TEST JAVA MOCK TEST II

Software Engineering Design & Construction

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

More about inheritance

Inheritance and object compatibility

Chapter 11: Collections and Maps

Inheritance. A key OOP concept

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

Type Hierarchy. Lecture 6: OOP, autumn 2003

25. Generic Programming

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

COSC This week. Will Learn

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

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

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London

Binghamton University. CS-140 Fall Dynamic Types

Inheritance. Inheritance

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

What is Inheritance?

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

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

Lecture 4: Extending Classes. Concept

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

Programming Languages and Techniques (CIS120)

INHERITANCE. Spring 2019

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

More on Inheritance. Interfaces & Abstract Classes

Inheritance: Definition

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

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

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

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

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

Principles of Software Construction: Objects, Design, and Concurrency. Part 1: Design for reuse. Design patterns for reuse

Transcription:

Principles of Software Construction: Objects, Design and Concurrency Polymorphism, part 2 15-214 toad Fall 2012 Jonathan Aldrich Charlie Garrod School of Computer Science 2012 C Garrod, J Aldrich, and W Scherlis

Key concepts from Thursday Packages Inheritance and polymorphism Subtyping toad 2

Subtyping «interface» Account getbalance() : float deposit(amount : float) withdraw(amount : float) : boolean transfer(amount : float, target : Account) : boolean monthlyadjustment() «interface» CheckingAccount getfee() : float # balance : float AbstractAccount + getbalance() : float + deposit(amount : float) + withdraw(amount : float) : boolean + transfer(amount : float, target : Account) : boolean + monthlyadjustment() «interface» SavingsAccount getinterestrate() : float monthlyadjustment() getfee() : float CheckingAccountImpl SavingsAccountImpl monthlyadjustment() getinterestrate() : float toad 3

Reuse via code wrappers, version 2: Delegation «interface» Account getbalance() : float deposit(amount : float) withdraw(amount : float) : boolean transfer(amount : float, target : Account) : boolean monthlyadjustment() requires two- way dependence BasicAccountImpl account adjustment «interface» Adjustment balance : float doadjust() getbalance() : float deposit(amount : float) withdraw(amount : float) : boolean transfer(amount : float, target : Account) : boolean monthlyadjustment() SavingsAccountAdjustment doadjust() adjustment!.doadjust()! float bal = account.getbalance();! float interest = bal * interestrate;! account.deposit(interest);! toad 4

Reuse via code wrappers, version 2: Delegation «interface» Account getbalance() : float deposit(amount : float) withdraw(amount : float) : boolean transfer(amount : float, target : Account) : boolean monthlyadjustment() public void adjustall(adjustment[] adjs) {! for (Adjustment a : adjs) {! a.doadjust();! BasicAccountImpl account adjustment «interface» Adjustment balance : float doadjust() getbalance() : float deposit(amount : float) withdraw(amount : float) : boolean transfer(amount : float, target : Account) : boolean monthlyadjustment() SavingsAccountAdjustment doadjust() adjustment!.doadjust()! float bal = account.getbalance();! float interest = bal * interestrate;! account.deposit(interest);! toad 5

Key concepts for today More inheritance and polymorphism Method dispatch, revisited super, this, final, instanceof, type casting Types and typechecking The java.lang.object! Type polymorphism toad 6

Method dispatch, revisited e.g.: x.foo(apple, 42)! Step 1 (compile time): determine which class to look in Here, the type of x! Step 2 (compile time): determine the method signature Find all accessible, applicable methods Select the most specific method m1 is more specific than m2 if each argument of m1 is a subtype of the corresponding argument of m2! toad 7

Method dispatch, revisited e.g.: x.foo(apple, 42)! Step 3 (run time): Determine the run-time class of the receiver Step 4 (run time): Locate the method to invoke Starting at the run-time class, look for a method with the right name and argument types that are identical to those in the method found statically (step 2) If it is found in the run-time class, invoke it. Otherwise, continue the search in the superclass of the run-time class I claim: this procedure will always find a method to invoke toad 8

Method dispatch practice public class GenericAnimal {! public String getnoise() { return Noise ; public class Bird extends GenericAnimal {! public String getnoise() { return Chirp ; public class Cat extends GenericAnimal {! public String getnoise() { return Meow ; public class GenericDog extends GenericAnimal {! // nothing special to hear here! public class Ewokian extends GenericDog {! public String getnoise() { return Oonga! ; toad 9

Method dispatch practice, part A public class GenericAnimal {! public String getnoise() { return Noise ; public class Bird extends GenericAnimal {! public String getnoise() { return Chirp ; public class Cat extends GenericAnimal {! public String getnoise() { return Meow ; public class GenericDog extends GenericAnimal {! // nothing special to hear here! public class Ewokian extends GenericDog {! public String getnoise() { return Oonga! ; What is printed by: GenericAnimal A = new GenericAnimal();! System.out.print(A.getNoise());!! toad 10

Method dispatch practice, part B-1 public class GenericAnimal {! public String getnoise() { return Noise ; public class Bird extends GenericAnimal {! public String getnoise() { return Chirp ; public class Cat extends GenericAnimal {! public String getnoise() { return Meow ; public class GenericDog extends GenericAnimal {! // nothing special to hear here! public class Ewokian extends GenericDog {! public String getnoise() { return Oonga! ; What is printed by: Bird B = new Bird();! System.out.print(B.getNoise());!! toad 11

Method dispatch practice, part B-2 (on paper!) public class GenericAnimal {! public String getnoise() { return Noise ; public class Bird extends GenericAnimal {! public String getnoise() { return Chirp ; public class Cat extends GenericAnimal {! public String getnoise() { return Meow ; public class GenericDog extends GenericAnimal {! // nothing special to hear here! public class Ewokian extends GenericDog {! public String getnoise() { return Oonga! ; What is printed by: GenericAnimal B = new Bird();! System.out.print(B.getNoise());!! toad 12

Method dispatch practice, part C public class GenericAnimal {! public String getnoise() { return Noise ; public class Bird extends GenericAnimal {! public String getnoise() { return Chirp ; public class Cat extends GenericAnimal {! public String getnoise() { return Meow ; public class GenericDog extends GenericAnimal {! // nothing special to hear here! public class Ewokian extends GenericDog {! public String getnoise() { return Oonga! ; What is printed by: GenericAnimal C = new Cat();! System.out.print(C.getNoise());!! toad 13

Method dispatch practice, part D public class GenericAnimal {! public String getnoise() { return Noise ; public class Bird extends GenericAnimal {! public String getnoise() { return Chirp ; public class Cat extends GenericAnimal {! public String getnoise() { return Meow ; public class GenericDog extends GenericAnimal {! // nothing special to hear here! public class Ewokian extends GenericDog {! public String getnoise() { return Oonga! ; What is printed by: GenericAnimal D = new GenericDog();! System.out.print(D.getNoise());!! toad 14

Method dispatch practice, part E-1 public class GenericAnimal {! public String getnoise() { return Noise ; public class Bird extends GenericAnimal {! public String getnoise() { return Chirp ; public class Cat extends GenericAnimal {! public String getnoise() { return Meow ; public class GenericDog extends GenericAnimal {! // nothing special to hear here! public class Ewokian extends GenericDog {! public String getnoise() { return Oonga! ; What is printed by: GenericAnimal E = new Ewokian();! System.out.print(E.getNoise());!! toad 15

Method dispatch practice, part E-2 public class GenericAnimal {! public String getnoise() { return Noise ; public class Bird extends GenericAnimal {! public String getnoise() { return Chirp ; public class Cat extends GenericAnimal {! public String getnoise() { return Meow ; public class GenericDog extends GenericAnimal {! // nothing special to hear here! public class Ewokian extends GenericDog {! public String getnoise() { return Oonga! ; What is printed by: Ewokian E = new Ewokian();! GenericAnimal F = E;! System.out.print(F.getNoise());! toad 16

Extended re-use with super! public abstract class AbstractAccount implements Account {!!protected float balance = 0.0;!!public boolean withdraw(float amount) {!! // withdraws money from account (code not shown)!!! public class ExpensiveCheckingAccountImpl!!!extends AbstractAcount implements CheckingAccount {!!public boolean withdraw(float amount) {!!!balance -= HUGE_ATM_FEE;!!!boolean success = super.withdraw(amount)!!!if (!success)!!!!balance += HUGE_ATM_FEE;!!!return success;!! 15-214 Garrod toad 17 Overrides withdraw but also uses the superclass withdraw method

Constructor calls with this and super! public class CheckingAccountImpl!!!!!!!extends AbstractAcount implements CheckingAccount {!!private float fee;!!public CheckingAccountImpl(float initialbalance, float fee) {!!!super(initialbalance);!!!this.fee = fee;!!!public CheckingAccountImpl(float initialbalance) {!!!this(initialbalance, 5.00);!!!/* other methods */ Invokes a constructor of the superclass. Must be the first statement of the constructor. Invokes another constructor in this same class toad 18

Inheritance Details: final! A final class: cannot extend the class e.g., public final class CheckingAccountImpl {! A final method: cannot override the method A final field: cannot assign to the field (except to initialize it) Why might you want to use final in each of the above cases? toad 19

Type-casting in Java Sometimes you want a different type than you have e.g.,!float pi = 3.14;!!int indianapi = (int) pi;! Useful if you know you have a more specific subtype: e.g., Account acct = ;! CheckingAccount checkingacct =! (CheckingAccount) acct;! float fee = checkingacct.getfee(); Will get a ClassCastException if types are incompatible toad 20

Inheritance Details: instanceof! Operator that tests whether an object is of a given class Account acct = ;! float adj = 0.0;! if (acct instanceof CheckingAccount) {! checkingacct = (CheckingAccount) acct;!! adj = checkingacct.getfee();! Advice: avoid instanceof if possible! toad 21

Typechecking The key idea: Analyze a program to determine whether each operation is applicable to the types it is invoked on Benefits: Finds errors early e.g., int h = hi / 2;! Helps document program code e.g., baz(frob) { /* what am I supposed to do!!!! with a frob? */ void baz(car frob) { /* oh, look,! I can drive it! */! toad 22

Value Flow and Subtyping Value flow: assignments, passing parameters e.g., Foo f = expression;! Determine the type T source of the source expression Determine the type T dest of the destination variable f! Check that T source is a subtype of T dest Subtype relation A <: B A <: B if A extends B or A implements B Means you can substitute a thing of type A for a thing of type B Subtypes are: Reflexive: A <: A Transitive: if A <: B and B <: C then A <: C toad 23

Typechecking expressions in Java Base cases: variables and fields the type is explicitly declared Expressions using new...()! the type is the class being created Type-casting the type is the type forced by the cast For method calls, e.g., e1.m(e2) 1. Determine the type T1 of the receiver expression e1 2. Determine the type T2 of the argument expression e2 3. Find the method declaration m in type T1 (or supertypes), using dispatch rules 4. The type is the return type of the method declaration identified in step 3 toad 24

Subtyping Rules If a concrete class B extends type A B must define or inherit all concrete methods declared in A If B overrides a method declared in supertype A The argument types must be the same as those in A s method The result type must be a subtype of the result type from A s method Behavioral subtyping If B overrides a method declared in A, it should conform to the specification from A If Cowboy.draw() overrides Circle.draw(), somebody gets hurt! toad 25

Key concepts for today More inheritance and polymorphism Method dispatch, revisited super, this, final, instanceof, type casting Types and typechecking The java.lang.object! Type polymorphism toad 26

The java.lang.object! All Java objects inherit from java.lang.object! Commonly-used/overridden public methods: String!toString()! boolean!equals(object obj)! int!hashcode()! Object!clone()! toad 27

Overriding java.lang.object s.equals! The default.equals: public class Object {! public boolean equals(object obj) {! return this == obj;! An aside: Do you like: public class CheckingAccountImpl! implements CheckingAccount {! @Override! public boolean equals(object obj) {! return false;! toad 28

The.equals(Object obj) contract An equivalence relation Reflexive: x! x.equals(x)! Symmetric: x,y! x.equals(y) if and only if y.equals(x)! Transitive: x,y,z x.equals(y) and y.equals(z) implies x.equals(z)! Consistent Invoking x.equals(y) repeatedly returns the same value unless x or y is modified x.equals(null) is always false toad 29