Subtyping Rules David Evans cs205: engineering software BlackBear

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

Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Identity and Equality Based on material by Michael Ernst, University of Washington.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 13: Concurring Concurrently.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 16: Concurrent Programming.
5/17/2015 OO Design: Liskov Substitution Principle 1.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
Introduction to Inheritance Fall 2005 OOPD John Anthony.
Exam Objective : Legal return types PRESENTED BY : SRINIVAS VG.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
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.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
OO subtyping A type SubA may only be a subtype of A if every instance of SubA can be safely substituted for any instance of A. –SubA instances must handle.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
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.
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
ECE122 Feb. 22, Any question on Vehicle sample code?
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
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.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
Difficult Specifications in Javari Immutability Inference Jaime Quinonez Program Analysis Group April 20, 2007.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
CSCI-383 Object-Oriented Programming & Design Lecture 24.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Cs2220: Engineering Software Class 13: Behavioral Subtyping Fall 2010 University of Virginia David Evans.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Behavioral Subtyping.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Polymorphism in Methods
Design David Evans cs205: engineering software
Inheritance and Polymorphism
CSC 205 Java Programming II
Coming up Constructors Overloading With one parameter
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Type Abstraction SWE Spring 2009.
Continuing Chapter 11 Inheritance and Polymorphism
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.
Overloading and Constructors
Extending Classes.
Type Abstraction Liskov, Chapter 7.
Building Java Programs
Object-Oriented Programming
More About Inheritance & Interfaces
Lecture 13: Subtyping Rules Killer Bear Climber
Subtype Substitution Principle
Lecture 9: When is S  T safe? Killer Bear
Type Abstraction SWE Spring 2013.
CS 240 – Advanced Programming Concepts
Presentation transcript:

Subtyping Rules David Evans cs205: engineering software BlackBear university of virginia fall 2006 Subtyping Rules Killer Bear Climber KillingBear BlackBear GrizzlyBear David Evans www.cs.virginia.edu/cs205

Recap If B is a subtype of A, everywhere an A is expected, a B can be used To implement a subtype, it is often useful to use the implementation of its supertype class B extends A B is a subtype of A B inherits from A class C implements F C is a subtype of F

Behavioral Subtyping? Killer Bear What’s the difference between a Black Bear and a Grizzly Bear? Climber KillingBear When you climb up the tree, the Black Bear climbs up after you. The Grizzly Bear knocks down the tree. (Which is the behavioral subtype?) BlackBear GrizzlyBear

Subtyping Example public class Bear { public Bear () { } abstract public boolean chaseHuman (Human h); public void eatHuman (Human h) { if (chaseHuman (h)) say (“Yum Yum!”); } public void say (String s) { System.err.println (“Bear says: ” + s); public class BlackBear extends Bear { public boolean chaseHuman (Human h) { // follow h and climb the tree return true; } public void say (String s) { System.err.println (“BlackBear says: ” + s); BlackBear b = new BlackBear (); b.eatHuman (h);

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 17 23 true false static public void main (String args[]) { public class Overloaded { public int tryMe (Object o) { return 17; } public int tryMe (String s) { return 23; public boolean equals (String s) { return true; Overloading 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"))); } 17 23 true false

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 use static methods to create new objects)

How do we know if saying B is a subtype of A is safe?

Substitution Principle If B is a subtype of A, everywhere the code expects an A, a B can be used instead For a function f (A), if f satisfies its specification when passed an object whose actual type is type A, f also satisfies its specification when passed an object whose actual type is B.

What needs to be true? public int f (a A, x X) { return a.m (x); } For a function f (a A), if f satisfies its specification when passed an object whose actual type is type A, f also satisfies its specification when passed an object whose actual type is B. public int f (a A, x X) { return a.m (x); }

Subtype Condition 1: Signature Rule We can use a subtype method where a supertype methods is expected: Subtype must implement all of the supertype methods Argument types must not be more restrictive Result type must be at least as restrictive Subtype method must not throw exceptions that are not subtypes of exceptions thrown by supertype

Signature Rule class A { public RA m (PA p) ; } class B extends A { public RB m (PB p) ; } RB must be a subtype of RA: RB <= RA PB must be a supertype of PA: PB >= PA covariant for results, contravariant for parameters

Signature Examples public class Object { public boolean equals (Object o) ; } public class String extends Object { } public boolean equals (Object o) ;

Signature Examples public class CellSet { // A set of Cell’s. public Cell choose () throws NoSuchElementException ; } public class ConwayCellSet extends CellSet { // A set of ConwayCell (<= Cell) objects. public ConwayCell choose () } public Cell choose () ; Return type must be a subtype of Cell.

Java’s Rule Java compiler is stricter than this: doesn’t allow any variation in types (“novariant”): Overriding method must have same return and parameter types Overriding method must throw the same or fewer exceptions than supertype method.

What needs to be true? public int f (a A, x X) { For a function f (a A), if f satisfies its specification when passed an object whose actual type is type A, f also satisfies its specification when passed an object whose actual type is B. public int f (a A, x X) { // REQUIRES: a is initialized // EFFECTS: returns a.value * x.value return a.m (x); }

Substitution Principle Continues in class 15...