Liskov Substitution Principle

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Advertisements

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
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Testing and Inheritance What is the relation between the test suite of a superclass and a subclass?
Introduction to Object Oriented Programming Java.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
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.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
INT-Evry (Masters IT– Soft Eng)RegressionTesting.1 (OO) Regression Testing Regression testing is the execution of a set of test cases on a program.
Object-oriented metrics Design decisions: Class Cohesion Open-Closed Single Responsibility Interface Segregation Dependency Inversion Liskov Substitution.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Ch:10 Component Level Design Unit 4. What is Component? A component is a modular building block for computer software Because components reside within.
CSC 395 – Software Engineering Lecture 12: Reusability –or– Programming was Bjarne Again.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
CSE 301 Exam Revision Lecture
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
OO as a language for acm l OO phrase l Mental model of key concepts.
Software Design Principles
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
1 Software Engineering: A Practitioner’s Approach, 6/e Chapter 11a: Component-Level Design Software Engineering: A Practitioner’s Approach, 6/e Chapter.
Object Oriented Programming
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
Five design principles
Inspired by the Oulipu. The 3 Tenets of OO Encapsulation Polymorphism Inheritance.
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
CHAPTER 3 MODELING COMPONENT-LEVEL DESIGN.
SOLID Design Principles
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.
Object-Oriented Programming “The Rest of the Story”, CS 4450 – Chapter 16.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
OCP and Liskov Principles
Sections Inheritance and Abstract Classes
CHAPTER 5 GENERAL OOP CONCEPTS.
Interfaces Unit 08.
Interfaces.
Inheritance and Polymorphism
Advanced Programming in Java
3 Fundamentals of Object-Oriented Programming
Object Oriented Practices
lecture 08, OO Design Principle
Week 6 Object-Oriented Programming (2): Polymorphism
Subtype Polymorphism, Subtyping vs
Java Programming, Second Edition
COMPUTER 2430 Object Oriented Programming and Data Structures I
Inheritance.
The SOLID Principles.
A (partial) blueprint for dealing with change
Object-Oriented PHP (1)
1.
Some principles for object oriented design
Liskov Substitution Principle (LSP)
Chapter 10 – Component-Level Design
Presentation transcript:

Liskov Substitution Principle Guilherme Menezes

Definition Introduced by Barbara Liskov Substitution Principle Content: Introduced by Barbara Liskov It is a particular definition of subtyping in OO Programming Based on the idea of substitution Definition Example 1 Compliance Polymorphism Importance Example 2 Conclusion

Definition Liskov Substitution Principle Content: “If S is a subtype of T, then objects of type T may be replaced with objects of type S without altering the correctness of the program.” Definition Example 1 Compliance Polymorphism Importance Example 2 Conclusion

Example class W { (..) void use (T obj) { String s = obj.getInfo() } objW.use(objT) /* no error is introduced if S is compliant with the principle*/ objW.use(objS)

Compliance Liskov Substitution Principle Content: A subclass must keep all the external observable behaviour of its superclass A subclass may extend the external observable behavior, but never modify it External observable behaviour = behaviour of public methods Definition Example 1 Compliance Polymorphism Importance Example 2 Conclusion

LSP and Polymorphism Inclusion Polymorphism Liskov Substitution Principle Content: Inclusion Polymorphism “A subtype can be used where the type is expected.” The Liskov Substitution Principle helps to guarantee Inclusion Polymorphism Definition Example 1 Compliance Polymorphism Importance Example 2 Conclusion

Importance Inclusion Polymorphism improves reuse: Liskov Substitution Principle Content: Inclusion Polymorphism improves reuse: All code that references the superclass can be reused referencing a subclass Not compliant with LSP -> No reuse Definition Example 1 Compliance Polymorphism Importance Example 2 Conclusion

Importance Biggest problem: Liskov Substitution Principle Content: Biggest problem: Programmers that write client modules usually assume Inclusion Polymorphism when writing OO Programs If we don’t comply with LSP, we may compromise not only reusability, but also correctness! Definition Example 1 Compliance Polymorphism Importance Example 2 Conclusion

Example Liskov Substitution Principle Content: Definition Example 1 Compliance Polymorphism Importance Example 2 Conclusion

Example A programmer may write Liskov Substitution Principle Content: A programmer may write /*Method to print information about someone on the screen*/ void printInfo(Person obj) { (…) parser.parseName(obj.getName()); (…) } Definition Example 1 Compliance Polymorphism Importance Example 2 expecting this code to work for all subtypes of “Person”, including “Employee”. This code will actually result in error. The redefinition of “getName” changed the behavior of the method, and introduced an error (or several errors) in the program. Conclusion

Conclusion The LSP is very closely related to: Design by Contract Liskov Substitution Principle Content: The LSP is very closely related to: Design by Contract The Open-Closed Principle. Together, they form the basic guidelines of OO Design. Definition Example 1 Compliance Polymorphism Importance Example 2 Conclusion

Conclusion The LSP show us that, during a design, we should focus on: Liskov Substitution Principle Content: The LSP show us that, during a design, we should focus on: the external behavior of methods and assumptions the programmer of client modules will make. It also show us a way to use inheritance properly to achieve reuse and assure correctness. Definition Example 1 Compliance Polymorphism Importance Example 2 Conclusion

References Inclusion polymorphism: Liskov Substitution Principle: Content: Inclusion polymorphism: http://courses.cs.byu.edu/cs330/cs330/lectures/lect-31.php Liskov Substitution Principle: http://en.wikipedia.org/wiki/Liskov_substitution_principle http://citeseer.ist.psu.edu/liskov94family.html http://www.objectmentor.com/resources/articles/lsp.pdf Open-closed principle: http://en.wikipedia.org/wiki/Open/closed_principle Design by contract: http://en.wikipedia.org/wiki/Design_by_contract http://www.cs.cofc.edu/~bowring/classes/csci%20360/presentations/Rivera_Carlos_DesignByContract.ppt Definition Example 1 Compliance Polymorphism Importance Example 2 Conclusion