Typing Issues and LSP Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Readability (Java vs. Pearl) Catching errors.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Chapter 1 Inheritance University Of Ha’il.
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Chair of Software Engineering CAT calls (Changed Availability or Type) these slides contain advanced material and are optional.
CSE-321 Programming Languages Subtyping POSTECH May 14, 2007 박성우.
5/17/2015 OO Design: Liskov Substitution Principle 1.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?
. Virtual Classes & Polymorphism. Example (revisited) u We want to implement a graphics system u We plan to have lists of shape. Each shape should be.
1 CS4850 Programming Languages Wuwei Shen. 2 Aministrivia Course home page: Reference books: –Programming Languages,
More Inheritance and LSP CS340100, NTHU Yoshi. More about Inheritance Reuse? – Q1: 你有沒有程式 ” 砍掉重練 ” 的經驗 ? – Q2: 你有沒有 ” 再造輪子 ” 的經驗 ? class Rectangle – Firstly,
Exceptions Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Covariance\Contravariance Reminder class Parent { void test (covar : Mammal,
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lesson 21 Last update: 25 May 2004 Type issues, covariance and catcalls.
1 Advanced Material The following slides contain advanced material and are optional.
Review Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Review What have we done during the course? Where to learn more? What is for the.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
1 CS4850 Programming Languages Wuwei Shen. 2 Aministrivia Course home page: Reference books: –Programming Languages,
Chair of Software Engineering ATOT - Lecture 23, 23 June Advanced Topics in Object Technology Bertrand Meyer.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
Typing Issues and LSP David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Reliability Catching errors early Readability.
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CSc 335: Three More OO Design Principles
Design II Today: Design Principles…can we define them intro.12.ppt CS 121 “Ordering Chaos” “Mike” Michael A. Erlinger.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
More Design. Next Tuesday First draft of architectural design –use cases –class diagrams for major classes with responsibilities –sequence diagrams for.
Programming Language C++ Xulong Peng CSC415 Programming Languages.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Type Conformance Contravariance, Covariance & Closed behavior CSC 422 Dr. Spiegel.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
Design Principles iwongu at gmail dot com.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Software Design Principles
1 Single Responsibility Principle Open Closed Principle Liskov Substitution Principle Law of Demeter CSC 335: Object-Oriented Programming and Design.
Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?
Types in programming languages1 What are types, and why do we need them?
Chapter FifteenModern Programming Languages1 A Second Look At Java.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
Conformance Object-Oriented Programming Spring
1. Perspectives on Design Principles – Semantic Invariants and Design Entropy Catalin Tudor 2.
Ceg860 (Prasad)L17IT1 Inheritance Techniques Subcontracting Anchored Types.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Test Code Patterns How to design your test code. 2 Testing and Inheritance Should you retest inherited methods? Can you reuse superclass tests for inherited.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Subtying Kangwon National University 임현승 Programming Languages These slides were originally created by Prof. Sungwoo Park at POSTECH.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Modern Programming Tools And Techniques-I
More Design Heuristics
Inheritance Techniques
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
EECE 310: Software Engineering
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Verified Subtyping with Traits and Mixins
COMPUTER 2430 Object Oriented Programming and Data Structures I
Liskov Substitution Principle (LSP)
Computer Science II for Majors
Presentation transcript:

Typing Issues and LSP Amit Shabtay

March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Readability (Java vs. Pearl) Catching errors early Reliability Efficiency Dynamic typing Flexibility Speed

March 3rd, 2004 Object Oriented Design Course 3 Weak Typing Flexibility (Python) class Cat: def speak(self): print "meow!" class Dog: def speak(self): print "woof!“ class Bob: def speak(self): print "hello, world!“ def command(pet): pet.speak() pets = [ Cat(), Dog(), Bob() ] for pet in pets: command(pet)

March 3rd, 2004 Object Oriented Design Course 4 Typing “Programmer cycles are expensive, CPU cycles are cheap” (Bruce Eckel)(Bruce Eckel Pro or Con static typing? How do we add flexibility to static typing? Genericity – C++ templates, Java Generics Inheritance (including multiple inheritance)

March 3rd, 2004 Object Oriented Design Course 5 Covariance, Contravariance and NoVariance Covariance: The policy allowing a feature redeclaration to change the signature so that the new types of both arguments and result conform to the originals (inherited). Contravariance: The policy allowing a feature redeclaration to change the signature so that a new result type will conform to the original but the original argument types conform to the new. Novariance: The policy prohibiting feature redeclaration from changing the signature.

March 3rd, 2004 Object Oriented Design Course 6 Covariance, Contravariance and NoVariance Covariance in method arguments: the ability to strengthen the type of an argument of a method. Contravariance: the ability to weakening the type of an argument. Novariance: the type can neither be strengthened nor weakened.

March 3rd, 2004 Object Oriented Design Course 7 class Parent { void test (covar : Mammal, contravar : Mammal) : boolean } class Child extends Parent { void test (covar : Cat, contravar : Animal) : boolean }

March 3rd, 2004 Object Oriented Design Course 8 Covariance Class Skier { Skier roommate; void share(Skier s); } Class Girl { Girl roommate; void share(Girl g); } Class RankedGirl { RankedGirl roommate; void share(RankedGirl rg); } Skier BoyGirl RankedBoyRankedGirl

March 3rd, 2004 Object Oriented Design Course 9 Covariance What happens when we do the following Skier s; Boy b; Girl g; b = new Boy(); g = new Girl(); s = b; s.share(g); g.share(b);

March 3rd, 2004 Object Oriented Design Course 10 Covariance What about multiple hierarchies? Does it solve the problem? Skier Girl RankedGirl Room GirlRoom RGRoom

March 3rd, 2004 Object Oriented Design Course 11 Descendant Hiding How can a class hide its parent’s method? Polygon + addVertex() Rectangle - VERTICES=4 - addVertex() Bird + fly() Ostrich - fly()

March 3rd, 2004 Object Oriented Design Course 12 Solutions Some languages allow you to redeclare methods (Eiffel) No Girl.share(Skier) Java and C++ do not allow this Need to check the validity at runtime if(skier instnaceof Girl) {…} if(skier instnaceof Girl) {…} else throw…

March 3rd, 2004 Object Oriented Design Course 13 The Liskov Substitution Principle If for each object o 1 of type S there is an object o 2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o 1 is substituted for o 2 then S is a subtype of T. Barbara Liskov, 1988

March 3rd, 2004 Object Oriented Design Course 14 LSP in plain English Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it

March 3rd, 2004 Object Oriented Design Course 15 What’s wrong with this? void DrawShape(const Shape& s) { if (typeid(s) == typeid(Square)) DrawSquare(static_cast (s)); else if (typeid(s) == typeid(Circle)) DrawCircle(static_cast (s)); }

March 3rd, 2004 Object Oriented Design Course 16 Things Are Not Always That Simple Consider the following class: class Rectangle{ public: void SetWidth(double w) {_width=w;} void SetHeight(double h) {_height=w;} double GetHeight() const {return _height;} double GetWidth() const {return _width;} private: double _width; double _height; };

March 3rd, 2004 Object Oriented Design Course 17 Square We want to add a Square object Naturally derives Rectangle And the trivial implementation is: void Square::SetWidth(double w) { Rectangle::SetWidth(w); Rectangle::SetHeight(w); } void Square::SetHeight(double h) { Rectangle::SetHeight(h); Rectangle::SetWidth(h); } Do you see any problem ?

March 3rd, 2004 Object Oriented Design Course 18 LSP is broken! void g(Rectangle& r) { r.SetWidth(5); r.SetHeight(4); assert(r.GetWidth()*r.GetHeight())==20); } A Square object is not Rectangle object! Their behavior is different

March 3rd, 2004 Object Oriented Design Course 19 The Liskov Substitution Principle Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it Use inheritance carefully!

March 3rd, 2004 Object Oriented Design Course 20 Design By Contract A way to define the behavior of classes in hierarchy A derived class must obey the contract of it’s parent class A contract can be “among gentlemen” or can be enforced using assertions and reflection.