1.

Slides:



Advertisements
Similar presentations
Design by Contract.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
1 Computer Science 340 Software Design & Testing Inheritance & Design By Contract.
Design by Contract. Design by contract is the process of developing software based on the notion of contracts between objects, which are expressed as.
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
5/17/2015 OO Design: Liskov Substitution Principle 1.
Error Management with Design Contracts Karlstad University Computer Science Error Management with Design Contracts Eivind J. Nordby, Martin Blom, Anna.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Paper Title: On the Precise Meaning of the OCL Constraints Presented by Alla Dove.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
On the Relation between Design Contracts and Errors Karlstad University Computer Science On the Relation Between Design Contracts and Errors A Software.
Liskov Substitution Principle
Introduction to Inheritance Fall 2005 OOPD John Anthony.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
Object-Oriented Design Justin Callanan CS 597. Object-Oriented Basics ● Data is stored and processed in “objects” ● Objects represent generalized real.
CSC 480 Software Engineering Design by Contract. Detail Design Road Map Begin with architectural models  Class model: domain classes  Overall state.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1. Perspectives on Design Principles – Semantic Invariants and Design Entropy Catalin Tudor 2.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
SOLID Design Principles
Essential Ada Terminology copyright © Michael B. Feldman, All Rights Reserved.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
SOLID PHP & Code Smell Wrap-Up
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Liskov Substitution Principle Jon McBee CLA, CLED, CTD, CPI, LabVIEW Champion.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Sections Inheritance and Abstract Classes
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Inheritance and Polymorphism
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
ATS Application Programming: Java Programming
Implementing Abstract Classes
Subtyping Rules David Evans cs205: engineering software BlackBear
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
11/29/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Object Oriented Practices
EECE 310: Software Engineering
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 9 Carrano Chapter 10 Small Java
Inheritance and Polymorphism
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Lecture 13: Subtyping Rules Killer Bear Climber
Dependency Inversion principle
Interface Segregation Principle
Liskov Substitution Principle (LSP)
Presentation transcript:

1

Liskov Substitution principle The principle was introduced by Barbara Liskov in her conference keynote “Data abstraction” in 1987. A few years later, she published a paper with Jeanette Wing in which they defined the principle as: Let Φ(x) be a property provable about objects x of type T. Then Φ(y)should be true for objects y of type S where S is a subtype of T. The principle defines that objects of a superclass shall be replaceable with objects of its subclasses without breaking the application. That requires the objects of your subclasses to behave in the same way as the objects of your superclass. www.seavus.com

The importance of LSP becomes obvious when you face the consequences of its violation. If class has not been defined according to LSP, its necessary to go through the existing code and account for the special cases involving the delinquent subclasses, which breaks Open-Closed principle. www.seavus.com

LSP compliance can be achieved by following a few rules, which are pretty similar to the Design By Contract concept defined by Bertrand Meyer. Pre-conditions cannot be strengthened. An overridden method of a subclass needs to accept the same input parameter values as the method of the superclass. Post-conditions cannot be weakened. Same rules that are applied for method output must be applied for overridden method. Invariants must be preserved. The most difficult and painful constraint to fulfill. Invariants are some time hidden in the base class and the only way to reveal them is to read the code of the base class. www.seavus.com

The Example public class Bird { public void fly(){} public class BirdTest{ public static void main(String[] args){     List<Bird> birdList = new ArrayList<Bird>();     birdList.add(new Bird());     birdList.add(new Crow());     birdList.add(new Ostrich());     letTheBirdsFly ( birdList );   }   static void letTheBirdsFly ( List<Bird> birdList ){    for ( Bird b : birdList ) {      b.fly(); } public class Bird { public void fly(){} public void eat(){} } public class Crow extends Bird {} public class Ostrich extends Bird{ public  void fly(){ throw new UnsupportedOperationException();   } www.seavus.com

public class Bird { public void eat(){} } public class FlyingBird extends Bird { public void fly(){} public class Crow extends FlyingBird {} public class Ostrich extends Bird {} Model your classes based on behaviors not on properties; model your data based on properties and not on behaviors. www.seavus.com

Summary It extends the Open/Closed principle and enables you to replace objects of a parent class with objects of a subclass without breaking the application. This requires all subclasses to behave in the same way as the parent class. To achieve that it’s necessary to follow these rules: Don’t implement any stricter validation rules on input parameters than implemented by the parent class. Apply at the least the same rules to all output parameters as applied by the parent class. No new exceptions should be thrown in derived class. www.seavus.com

Thank you for your attention Copyright: © 2018 Seavus. All rights reserved. | www.seavus.com | info@seavus.com | sales@seavus.com