Cs2220: Engineering Software Class 13: Behavioral Subtyping Fall 2010 University of Virginia David Evans.

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 13: Concurring Concurrently.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 16: Concurrent Programming.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Cs2220: Engineering Software Class 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 18: Behavioral Subtyping and.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Class 37: Computability in Theory and Practice cs1120 Fall 2011 David Evans 21 November 2011 cs1120 Fall 2011 David Evans 21 November 2011.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Class 14: Object-Oriented Programming Fall 2010 University of Virginia David Evans cs2220: Engineering Software.
Lecture 2: Classes and Objects, using Scanner and String.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Types in programming languages1 What are types, and why do we need them?
Fall 2010 UVa David Evans cs2220: Engineering Software Class 27: Exam 2.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 17: Inheritance & Behavioral.
Cs205: engineering software university of virginia fall 2006 David Evans Substitution Principle.
Type Abstraction Liskov, Chapter 7. 2 Liskov Substitution Principle In any client code, if the supertype object is substituted by a subtype object, the.
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
Cs205: engineering software university of virginia fall 2006 Introducing Java David Evans Don’t forget to your registration.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Cs205: engineering software university of virginia fall 2006 David Evans Object-Oriented Programming.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Information and Computer Sciences University of Hawaii, Manoa
The eclipse IDE IDE = “Integrated Development Environment”
Design David Evans cs205: engineering software
Inheritance and Polymorphism
CS216: Program and Data Representation
Lecture 17: Polymorphism (Part II)
Type Abstraction SWE Spring 2009.
Getting Started ARCS Lab..
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Subtyping Rules David Evans cs205: engineering software BlackBear
Concurring Concurrently
Type Abstraction Liskov, Chapter 7.
Lecture 10: Using Object-Oriented Languages
Inheritance in Java CS 3331 Fall 2009.
More on Creating Classes
Chapter 11 Inheritance and Polymorphism Part 2
Java’s Central Casting
Lecture 13: Subtyping Rules Killer Bear Climber
Type Abstraction SWE Spring 2013.
Review for Midterm 3.
CS 240 – Advanced Programming Concepts
Presentation transcript:

cs2220: Engineering Software Class 13: Behavioral Subtyping Fall 2010 University of Virginia David Evans

What’s the difference between a black bear and a grizzly bear?

Killer BlackBear GrizzlyBear Climber Bear KillingBear

Exam 1

Question 1 Give one concrete example where the Java programming language designers sacrificed expressiveness for truthiness. An ideal answer would illustrate your example with code snippets showing something that is difficult to express concisely because of the Java language’s emphasis on truthiness. public class HelloWorld { public static void main (String [] args) { System.out.println(“Hello!”); } What are the language design decisions Java made differently from Scheme to explain why this is so long?

What Java language design decisions make this so long? public class HelloWorld { public static void main (String [] args) { System.out.println(“Hello!”); }

Question 1 public class HelloWorld { public static void main (String [] args) { System.out.println(“Hello!”); } 1.Static typing: big win for truthiness 2.All procedures must be inside a class 3.Default visibility is not public (package protected) 4.Use squiggly brackets to denote blocks, semi-colons to end statements 5.Not providing a special, convenient way to print output, but requiring an I/O object and invoking a method

Exam <=704 Score Distribution I will re-ask (in slightly different from) at least some of the questions on Exam 1 on Exam 2.

Recap: Substitution Principle Summary Param Types Psub  Psuper Preconditions pre_sub  pre_super Result TypeRsub  Rsuper Postconditions post_sub  post_super Propertiesproperties_sub  properties_super contravariant for inputs covariant for outputs These properties ensure code that is correct using an object of supertype is correct using an object of subtype.

Killer BlackBear GrizzlyBear Climber Bear KillingBear What should the spec of the KillingBear class’ kill (Object o) method be? GrizzlyBear: public boolean kill(Object o) // MODIFIES: this, o, nearby trees and // incidentals // EFFECTS: If o is up a tree, knocks down // the tree, eats o, and returns true. // Otherwise, returns false. // REQUIRES: o is yummy to a GrizzlyBear BlackBear: public boolean kill(Object o) // MODIFIES: this, o // EFFECTS: If o is up a tree, climbs the tree, // eats o, and returns true. Otherwise, // if o is reachable, eats o. Otherwise, // returns false.

Substitution Principle Is this the only way?

Eiffel’s Rules (Described in Bertrand Meyer paper for ps4)

Eiffel Rules Skier set_roommate (Skier) Skier set_roommate (Skier) Boy Girl The types of the parameters in the subtype method may be subtypes of the supertype parameters. How can Girl override set_roomate? set_roommate (Girl g) set_roommate (Boy b) Opposite of substitution principle!

Eiffel and I Can’t Get Up? Skier set_roommate (Skier) Skier set_roommate (Skier) Boy Girl set_roomate (Girl) Girl set_roomate (Girl) Meyer’s paper is all about the contortions Eiffel needs to deal with non-substitutable subtypes s: skier; g: girl; b: boy; s := g;... s.set_roommate (b);

Substitution Principle vs. Eiffel Substitution PrincipleEiffel Parameters PB >= PAPB <= PA Preconditions pre_A  pre_Bpre_B  pre_A Result RB <= RARB <= RA Postconditions post_B  post_A post_B  post_A Skier set_roommate (Skier) Skier set_roommate (Skier) Boy set_roomate (Boy) Boy set_roomate (Boy) Skier set_roommate (Skier) Skier set_roommate (Skier) Boy set_roomate (Object) Boy set_roomate (Object)

Substitution Rules vs. Java Skier set_roommate (Skier) Skier set_roommate (Skier) Boy set_roomate (Object) Boy set_roomate (Object) Skier set_roommate (Skier) Skier set_roommate (Skier) void set_roommate(Skier) // Overloads void set_roommate (Object) void set_roommate(Skier) // Overloads void set_roommate (Object)

Overloading and Overriding Overriding: replacing a supertype’s method in a subtype – Dynamic dispatch finds method of actual type Overloading: providing two methods with the same name but different parameter types – Statically select most specific matching method of apparent type

Overloading Example public class Overloaded extends Object { public int tryMe (Object o) { return 17; } public int tryMe (String s) { return 23; } public boolean equals (String s) { return true; } public boolean equals (Object) is inherited from Object

Overloading public class Overloaded { public int tryMe (Object o) { return 17; } public int tryMe (String s) { return 23; } public boolean equals (String s) { return true; } static public void main (String args[]) { Overloaded over = new Overloaded (); System.err.println (over.tryMe (over)); System.err.println (over.tryMe (new String ("test"))); Object obj = new String ("test"); System.err.println (over.tryMe (obj)); System.err.println (over.equals (new String ("test"))); System.err.println (over.equals (obj)); Object obj2 = over; System.err.println (obj2.equals (new String ("test"))); } true false

Overloading 2 public class Overwhelming { public int tryMe (Object o, String s) { return 17; } public int tryMe (String s, Object o) { return 23; } public static void main(String[] args) { Overwhelming over = new Overwhelming (); System.err.println (over.tryMe ("test1", "test2")); } Compiler error: The method tryMe(Object, String) is ambiguous for the type Overwhelming Compiler error: The method tryMe(Object, String) is ambiguous for the type Overwhelming

Overkill Overloading and overriding together can be overwhelming! Avoid overloading whenever possible: names are cheap and plentiful One place you can’t easily avoid it: constructors (they all have to have the same name) – But, can make static “factory” methods instead (this is usually better) annotations so compiler will check that you are actually overriding!

Java Buzzword Description “A simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, and dynamic language.” [Sun95] Later in the course, we will discuss how well it satisfies these “buzzwords”. from Class 2...