5/17/2015 OO Design: Liskov Substitution Principle 1.

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
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.
Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?
Chapter 8 Designing Classes. Assignment Chapter 9 Review Exercises (Written)  R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Friday,
1 CS4850 Programming Languages Wuwei Shen. 2 Aministrivia Course home page: Reference books: –Programming Languages,
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
More Inheritance and LSP CS340100, NTHU Yoshi. More about Inheritance Reuse? – Q1: 你有沒有程式 ” 砍掉重練 ” 的經驗 ? – Q2: 你有沒有 ” 再造輪子 ” 的經驗 ? class Rectangle – Firstly,
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Typing Issues and LSP Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Readability (Java vs. Pearl) Catching errors.
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
Object-Oriented Analysis and Design Lecture 8 State Space, Behavior, and Type Conformance.
1 CS4850 Programming Languages Wuwei Shen. 2 Aministrivia Course home page: Reference books: –Programming Languages,
Typing Issues and LSP David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 Typing Static typing Reliability Catching errors early Readability.
Chapter 10 Classes Continued
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.
1 OO Design Principles Project Group eXtreme Programming Md. Abul Bashar 07/09/2004.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
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.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
More Design. Next Tuesday First draft of architectural design –use cases –class diagrams for major classes with responsibilities –sequence diagrams for.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Software Design Principles
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?
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 17: Inheritance & Behavioral.
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Subtype Polymorphism, Subtyping vs
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
Software Design and Implementation: Composition and Substitution Dror Feitelson Software Engineering Hebrew University.
Conformance Object-Oriented Programming Spring
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
1. Perspectives on Design Principles – Semantic Invariants and Design Entropy Catalin Tudor 2.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
CSCI-383 Object-Oriented Programming & Design Lecture 24.
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.
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Software Design Principles
CMPE 135: Object-Oriented Analysis and Design October 3 Class Meeting
Adaptive Code Via C#
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
Type Abstraction SWE Spring 2009.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Subtype Polymorphism, Subtyping vs
Subtyping Rules David Evans cs205: engineering software BlackBear
Software Design Principles
Type Abstraction Liskov, Chapter 7.
Subtype Polymorphism, Subtyping vs
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Lecture 13: Subtyping Rules Killer Bear Climber
Subtype Substitution Principle
1.
Type Abstraction SWE Spring 2013.
Liskov Substitution Principle (LSP)
Presentation transcript:

5/17/2015 OO Design: Liskov Substitution Principle 1

5/17/2015 OO Design: Liskov Substitution Principle 2 Liskov Substitution Principle  But we must be careful when we implement subclass to ensure that we don’t violate the LSP public class Rectangle { private double width, height; Rectangle(double w, double h) { width = w; height = h; } public double area() { return width * height; } public void setWidth(double w) { width = w;} public void setHeight(double h){ height = h;} public double getWidth() { return width; } public double getHeight() { return height;} }

5/17/2015 OO Design: Liskov Substitution Principle 3 LSP Example public class Square extends Rectangle { Square(double s) { super(s,s); } public void setWidth(double w) { super.setWidth(w); super.setHeight(w);} public void setHeight(double h){ super.setWidth(h); super.setHeight(h);} }

5/17/2015 OO Design: Liskov Substitution Principle 4 LSP Example public class TestRectangle { public static void testLSP(Rectangle r) { r.setWidth(4.0); r.setHeight(5.0); System.out.println(r.area()); assert(r.area()= 20); }

5/17/2015 OO Design: Liskov Substitution Principle 5 What Went Wrong?  Consider the postcondition for setWidth in classes Rectangle and Square:  Rectangle: width == w && height == old.height  Square: width == w && height == w  The postcondition for Square is weaker than the postcondition for Rectangle because it does not attempt to enforce the clause (height == old.height)

5/17/2015 OO Design: Liskov Substitution Principle 6 Rule  When you override a method in a base class, the precondition of the overriding method should be weaker than the precondition of the overriden method: The more useful a procedure is for clients The more difficult it is to implement correctly “Weaker” means the derived class should consider more inputs to the overriding method

5/17/2015 OO Design: Liskov Substitution Principle 7 Rule  When you override a method in a base class, the postcondition of the overriding method should be stronger than the postcondition of the overriden method: “Stronger” means the derived class should produce less outputs from the overriding method

5/17/2015 OO Design: Liskov Substitution Principle 8 So, void testRectangleSetWidth(Rectangle rec) { double oldHeight = rec.getHeight(); rec.setWidth(5); // postconditions of Rectangle assertTrue (5 == rec.getWidth() && oldHeight == rec.getHeight()); }

5/17/2015 OO Design: Liskov Substitution Principle 9 Subtype Substitution  If B is a subtype of A, everywhere the code expects an A, a B can be used instead  Examples: Cell c = c1; c1 must be a subtype of Cell (note A is a subtype of A) Cell c = new ConwayLifeCell (); ConwayLifeCell c = new Cell ();

5/17/2015 OO Design: Liskov Substitution Principle 10 Inheritance  To implement a subtype, it is often useful to use the implementation of its supertype  This is also called “subclassing” class B extends A B is a subtype of A B inherits from A class C implements F C is a subtype of F both subtyping and inheritance just subtyping No way to get inheritance without subtyping in Java

5/17/2015 OO Design: Liskov Substitution Principle 11 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 and the program still satisfies its specification

5/17/2015 OO Design: Liskov Substitution Principle 12 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

5/17/2015 OO Design: Liskov Substitution Principle 13 Signature Rule class A { public R A m (P A p) ; } class B extends A { public R B m (P B p) ; } R B must be a subtype of R A : R B <= R A P B must be a supertype of P A : P B >= P A

5/17/2015 OO Design: Liskov Substitution Principle 14 Substitution Mystery …(in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; …(anything could be here) mt1 = mt2.m (mt3); If the Java compiler accepts this code, which of these are guaranteed to be true: a.The static type of mt2 is MysteryType2 b.At the last statement, the run-time type of mt2 is MysteryType2 c.MysteryType2 has a method named m d.The MysteryType2.m method takes a parameter of type MysteryType3 e.The MysteryType2.m method returns a subtype of MysteryType1 f.After the last statement, the run-time type of mt1 is MysteryType1

5/17/2015 OO Design: Liskov Substitution Principle 15 …(in client code) MysteryType1 mt1; MysteryType2 mt2; MysteryType3 mt3; …(anything could be here) mt1 = mt2.m (mt3); a.The static type of mt2 is MysteryType2 b.At the last statement, the run-time type of mt2 is MysteryType2 c.MysteryType2 has a method named m d.The MysteryType2.m method takes a parameter of type MysteryType3 e.The MysteryType2.m method returns a subtype of MysteryType1 f.After the last statement, the run-time type of mt1 is MysteryType1 TRUE: the static type is obvious from the declaration. FALSE: we only know the run-time type <= MysteryType2 TRUE FALSE: we only know it takes a parameter >= MysteryType3 TRUE: the assignment type checking depends on this FALSE: we only know that the run-time type <= MysteryType1