Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

Similar documents
n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

Lecture 11 Subtypes and Subclasses

CSE 331 Software Design and Implementation. Lecture 12 Subtypes and Subclasses

Announcements. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Intuition: Type Signature is a Specification.

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation

MIT EECS Michael Ernst Saman Amarasinghe

OO Class Design Principles

Subtypes. CSE 331 University of Washington. Michael Ernst

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns

CS 520 Theory and Practice of Software Engineering Fall 2018

CS 520 Theory and Practice of Software Engineering Fall 2017

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

Subtypes and Subclasses

Inheritance and object compatibility

Exam 2. Topics. Preconditions vs. Exceptions. Exam 2 Review. Exam 2 on Thursday, March 29 th. Closed book, closed computer, closed phone

CS 320 Introduction to Software Engineering Spring March 06, 2017

Open Closed Principle (OCP)

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CS 320 Introduction to Software Engineering Spring March

17.11 Bean Rules persistent

Software Engineering Design & Construction

Principles of Object-Oriented Design

Liskov Substitution Principle

Java Object Oriented Design. CSC207 Fall 2014

CS-202 Introduction to Object Oriented Programming

Principles of OO Design

Introduction to Object-Oriented Programming

Intro to: Design Principles

Inheritance, Polymorphism, and Interfaces

CSC207 Week 3. Larry Zhang

Originality is Overrated: OO Design Principles

OO Design Principles

Object Oriented Software Design - I

Polymorphism and Interfaces. CGS 3416 Spring 2018

ITI Introduction to Computing II

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Announcements. Specifications. Outline. Specifications. HW1 is due Thursday at 1:59:59 pm

Chapter 10 Classes Continued. Fundamentals of Java

ITI Introduction to Computing II

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

OO Technology: Properties and Limitations for Component-Based Design

Conformance. Object-Oriented Programming Spring 2015

Today's Agenda. References. Open Closed Principle. CS 247: Software Engineering Principles. Object-Oriented Design Principles

6.170 Recitation #5: Subtypes and Inheritance

Bruno Bossola SOLID Design Principles

8. Polymorphism and Inheritance

Test Code Patterns. How to design your test code

Testing and Inheritance. Test Code Patterns. Inheritance-related bugs. Inheritance. Testing of Inheritance. Inheritance-related bugs

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Advanced Placement Computer Science. Inheritance and Polymorphism

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

OBJECT ORİENTATİON ENCAPSULATİON

index.pdf January 21,

Design Patterns So Far. Design Patterns, cont. Design Patterns So Far. Outline

Design Principles: Part 2

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

ECE 3574: Dynamic Polymorphism using Inheritance

C18a: Abstract Class and Method

Outline. Design Principles: Part 2. e.g. Rectangles and Squares. The Liskov Substitution Principle (LSP) ENGI 5895: Software Design.

ITI Introduction to Computing II

COURSE 2 DESIGN PATTERNS

Design Principles: Part 2

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

ITI Introduction to Computing II

Software Engineering

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Type Analysis. Type Checking vs. Type Inference

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Practice for Chapter 11

Object-Oriented Concepts and Design Principles

CS 370 Design Heuristics D R. M I C H A E L J. R E A L E F A L L

Object-Oriented Design II

1 Introduction 2 Complex Systems 3 Object Model 4 Dependency Management 5 Class Design. Object Oriented Programming in Physics

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Goals of the Lecture OO Programming Principles

Lecture: Modular Design

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Testing Object-Oriented Software. 22 November 2017

CSE 307: Principles of Programming Languages

Polymorphism CSCI 201 Principles of Software Development

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

The Liskov Substitution Principle

Chapter 9 Inheritance

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Concepts of Programming Languages

Software Design and SOLID Principles

So far. Specifications, conclusion. Abstract Data Types (ADTs) Comparison by Logical Formulas. Outline

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Chapter 11 Classes Continued

CSC207H: Software Design SOLID. CSC207 Winter 2018

Programming Languages Seminar. Lecture in the subject of. Yair Moshe

11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle

Introduction. Introduction. Introduction

Expanding Our Horizons. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011

Assertions. Assertions - Example

Topic 7: Algebraic Data Types

Software Engineering Design & Construction Dr. Michael Eichberg Fachgebiet Softwaretechnik Technische Universität Darmstadt

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Transcription:

Subtype, Subtyping vs. Subclassing, Liskov Substitution Principle Outline Subtype polymorphism Subtyping vs. subclassing Liskov Substitution Principle (LSP) Function subtyping Java subtyping Composition: an alternative to inheritance 2 Subtype Subtype polymorphism the ability to use a subclass where a superclass is expected Thus, dynamic method binding override A.m class A { void m() { class B extends A { void m() { class C extends A { void m() { Client: A a; a.m(); // Call a.m() can bind to any of A.m, B.m or C.m at runtime! Subtype polymorphism is the essential feature of object-oriented languages Java subtype: B extends A or B implements I A Java subtype is not necessarily a true subtype! 3 Example: Application draws shapes on screen Possible solution in C: enum ShapeType { circle, square ; struct Shape { ShapeType t ; struct Circle { ShapeType t; double radius; Point center; ; struct Square { ShapeType t; double side; Point topleft; ; 4 1

void DrawAll(struct Shape *list[], int n) { int i; for (i=0; i< n; i++) { struct Shape *s = list[i]; switch (s->t) { case square: DrawSquare(s); break; case circle: DrawCircle(s); break; What s really bad about this solution? Spring 18 CSCI 2600, K. Kuzmin, A Milanova 5 Example: OO Solution in Java: abstract class Shape { public void draw(); class Circle extends Shape { draw() class Square extends Shape { draw() class Triangle extends Shape { draw() void DrawAll(Shape[] list) { for (int i=0; i < list.length; i++) { Shape s = list[i]; s.draw(); Spring 18 CSCI 2600, K. Kuzmin, A Milanova 6 Enables extensibility and reuse In our example, we can extend Shape hierarchy with no modification to the client of hierarchy, DrawAll Thus, we can reuse Shape and DrawAll Subtype polymorphism enables the Open/closed principle Software entities (classes, modules) should be open for extension but closed for modification Credited to Bertrand Meyer Spring 18 CSCI 2600, K. Kuzmin, A Milanova 7 Science of software design teaches Design Patterns Design patterns promote design for extensibility and reuse Nearly all design patterns make use of subtype polymorphism Spring 18 CSCI 2600, K. Kuzmin, A Milanova 8 2

Outline Subtype polymorphism Subtyping vs. subclassing Liskov Substitution Principle (LSP) Function subtyping Java subtyping Composition: an alternative to inheritance What is True Subtyping? Subtyping, conceptually B is subtype of A means every B is an A In other words, a B object can be substituted where an A object is expected The notion of true subtyping connects subtyping in the real world with Java subtyping 9 Spring 18 CSCI 2600, K. Kuzmin, A Milanova 10 Examples of Subtypes Subset subtypes int is a subtype of real range [0..10] is a subtype of range [-10 10] Other subtypes Item Every book is a library item Every DVD is a library item Every triangle is a shape Shape Etc. Book DVD True Subtypes are Substitutable Subtypes are substitutable for supertypes Instances of subtypes won t surprise client by requiring more than the supertype Instances of subtypes won t surprise client by returning less than its supertype Java subtyping is realized through subclassing Java subtype is not the same as true subtype! Triangle Rombus Spring 18 CSCI 2600, K. Kuzmin, A Milanova 11 12 3

Subtyping and Subclassing Subtyping and substitutability --- specification notions B is a subtype of A if and only if a B object can be substituted where an A object is expected, in any context Subclassing and inheritance --- implementation notions B extends A, or B implements A B is a Java subtype of A, but not necessarily a true subtype of A! 13 True Subtype We say that (class) B is a true subtype of A if B has a stronger specification than A Heed when designing inheritance hierarchies! Java subtypes that are not true subtypes are confusing and dangerous Spring 18 CSCI 2600, K. Kuzmin, A Milanova 14 Subclassing. Inheritance Makes it Easy to Add Functionality class Product { private String title; private String description; private float price; public float getprice() { return price; public float gettax() { return getprice()*0.08f; and we need a class for Products that are on sale Spring 18 CSCI 2600, K. Kuzmin, A Milanova (slide due to Michael Ernst) 15 Code cloning is a bad idea! Why? class SaleProduct { private String title; private String description; private float price; private float factor; // extends Product public float getprice() { return price*factor; // extends Product public float gettax() { return getprice()*0.08f; Spring 18 CSCI 2600, K. Kuzmin, A Milanova (based on an example by Michael Ernst) 16 4

Subclassing What s a better way to add this functionality? class SaleProduct extends Product { private float factor; public float getprice() { return super.getprice()*factor; Subclassing keeps small extensions small Spring 18 CSCI 2600, K. Kuzmin, A Milanova (based on slide by Michael Ernst) 17 Benefits of Subclassing Don t repeat unchanged fields and methods Simpler maintenance: fix bugs once Differences are clear (not buried under mass of similarity!) Modularity: can ignore private fields and methods of superclass Can substitute new implementations where old one is expected (the benefit of subtype polymorphism) Another example: Timestamp extends Date Spring 18 CSCI 2600, K. Kuzmin, A Milanova (based on slide by Michael Ernst) 18 Subclassing Can Be Misused Poor planning leads to muddled inheritance hierarchies. Requires careful planning If a class is not a true subtype of its superclass, it can surprise client If class depends on implementation details of superclass, changes in superclass can break subclass. Fragile base class problem Spring 18 CSCI 2600, K. Kuzmin, A Milanova (based on a slide by Michael Ernst) 19 Classic Example of Subtyping vs. Subclassing: Every Square is a Rectangle, Thus, class Square extends Rectangle { But is a Square a true subtype of Rectangle? In other words, is Square substitutable for Rectangle in client code? class Rectangle { public void setsize(int w, int h); // returns: area of rectangle public int area(); Spring 18 CSCI 2600, K. Kuzmin, A Milanova 20 5

Every Square is a Rectangle, class Square extends Rectangle { // requires: w = h Choice 1: public void setsize(int w, int h); // effects: this post.width=w,this post.height=w Choice 2: public void setsize(int w, int h); // effects: this post.width=s,this post.height=s Choice 3: public void setsize(int s); // throws: BadSizeException if w!= h Choice 4: public void setsize(int w, int h); Spring 18 CSCI 2600, K. Kuzmin, A Milanova (based on a slide by Mike Ernst) 21 Every Square is a Rectangle, Choice 1 is not good It requires more! Clients of Rectangle are justified to have Rectangle r; r.setsize(5,4) In formal terms: spec of Square s setsize is not stronger than spec of Rectangle s setsize Because the precondition of Rectangle s setsize does not imply precondition of Square s setsize Thus, a Square can t be substituted for a Rectangle Spring 18 CSCI 2600, K. Kuzmin, A Milanova 22 Every Square is a Rectangle, Choice 4? It throws an exception that clients of Rectangle are not expecting and not handling Thus, a Square can t be substituted for a Rectangle Choice 3? Clients of Rectangle can write r.setsize(5,4). Square works with r.setsize(5) Choice 2? Client: Rectangle r; r.setsize(5,4); assert(r.area()==20) Again, Square surprises client with behavior that is different from Rectangle s 23 Every Square is a Rectangle, Square is not a true subtype of Rectangle Rectangles are expected to have height and width that can change independently Squares violate that expectation. Surprise clients Is Rectangle a true subtype of Square? No. Squares are expected to have equal height and width. Rectangles violate this expectation One solution: make them Shape unrelated Square Rectangle Spring 18 CSCI 2600, K. Kuzmin, A Milanova 24 6

Box is a BallContainer? class BallContainer { // modifies: this // effects: adds b to this container if b is not // already in // returns: true if b is added, false otherwise public boolean add(ball b); class Box extends BallContainer { // good idea? // modifies: this // effects: adds b to this Box if b is not // already in and this Box is not full // returns: true if b is added, false otherwise public boolean add(ball b); 25 Liskov Substitution Principle (LSP) Due to Barbara Liskov, Turing Award 2008 LSP: A subclass should be substitutable for superclass. I.e., every subclass should be a true subtype of its superclass Ensure that B is a true subtype of A by reasoning at the specification level B should not remove methods from A For each B.m that substitutes A.m, B.m s spec is stronger than A.m s spec Client: A a; a.m(int x,int y); Call a.m can 26 bind to B s m. B s m should not surprise client Exercise: Reason About Specs class Rectangle { public void setsize(int w, int h); class Square extends Rectangle { // requires: w = h public void setsize(int w, int h); Spring 18 CSCI 2600, K. Kuzmin, A Milanova (based on a slide by Mike Ernst) 27 Liskov Substitution Principle Java subtypes (realized with extends, implements) must be true subtypes Java subtypes that are not true subtypes are dangerous and confusing When B is a Java subtype of A, ensure B, does not remove methods from A A substituting method B.m has stronger spec than method A.m which it substitutes Guarantees substitutability 28 7