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

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

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

Subtypes and Subclasses

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

Inheritance and object compatibility

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

Open Closed Principle (OCP)

CSC207 Week 3. Larry Zhang

17.11 Bean Rules persistent

Java Object Oriented Design. CSC207 Fall 2014

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

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

Introduction to Object-Oriented Programming

CS 320 Introduction to Software Engineering Spring March

Principles of Object-Oriented Design

Liskov Substitution Principle

CS-202 Introduction to Object Oriented Programming

Software Engineering Design & Construction

Outline. Logistics. Logistics. Principles of Software (CSCI 2600) Spring Logistics csci2600/

ITI Introduction to Computing II

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

Inheritance, Polymorphism, and Interfaces

Principles of OO Design

6.170 Recitation #5: Subtypes and Inheritance

ITI Introduction to Computing II

Originality is Overrated: OO Design Principles

OO Design Principles

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

Object Oriented Software Design - I

Chapter 10 Classes Continued. Fundamentals of Java

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

C18a: Abstract Class and Method

Conformance. Object-Oriented Programming Spring 2015

Bruno Bossola SOLID Design Principles

Intro to: Design Principles

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

8. Polymorphism and Inheritance

OO Technology: Properties and Limitations for Component-Based Design

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Test Code Patterns. How to design your test code

Polymorphism and Interfaces. CGS 3416 Spring 2018

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

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

index.pdf January 21,

Goals of the Lecture OO Programming Principles

Announcements. Representation Invariants and Abstraction Functions. Announcements. Outline

ECE 3574: Dynamic Polymorphism using Inheritance

Design Principles: Part 2

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

ITI Introduction to Computing II

Design Principles: Part 2

ITI Introduction to Computing II

Object Oriented Programming: Based on slides from Skrien Chapter 2

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

Advanced Placement Computer Science. Inheritance and Polymorphism

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

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

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

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Type Analysis. Type Checking vs. Type Inference

OBJECT ORİENTATİON ENCAPSULATİON

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

Programming Languages and Techniques (CIS120)

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

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

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

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

Announcements. Testing. Announcements. Announcements

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

Testing Object-Oriented Software. 22 November 2017

Design by Contract: An Overview

Polymorphism CSCI 201 Principles of Software Development

COURSE 2 DESIGN PATTERNS

Programming in the large

CSCI 2600: Principles of Software. Spring 2017 Lecture 01 Bill Thompson

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

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

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Outline. Software Rots

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

!" #$ +-,.-% $/00&&& $ 0$0

The Liskov Substitution Principle

Inheritance Abstraction Polymorphism

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

Chapter 9 Inheritance

Object Oriented Programming and Design in Java. Session 10 Instructor: Bert Huang

Solutions to Quiz 1 (March 14, 2016)

Transcription:

Subtype, Subtyping vs. Subclassing, Liskov Substitution Principle Announcements 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 n HW4 resubmit due Friday October 27 th 2pm n A New Submitty page!!! n Auto-graded part of HW4 will be the average of the two submissions n PULL hw5 before you COMMIT/PUSH!!! n Quiz 6 today 2 Outline of today s class Subtype the ability to use a subclass where a superclass is expected n Thus, dynamic method binding override A.m n class A { void m() { n class B extends A { void m() { n class C extends A { void m() { n Client: A a; a.m(); // Call a.m() can bind to any of A.m, B.m or C.m at runtime! is the essential feature of object-oriented languages n Java subtype: B extends A or B implements I n A Java subtype is not necessarily a true subtype! 3 4 n Example: Application draws shapes on screen n 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; ; 5 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? Fall 17 CSCI 2600, A Milanova 6 1

n 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(); Fall 17 CSCI 2600, A Milanova 7 n Enables extensibility and reuse n In our example, we can extend Shape hierarchy with no modification to the client of hierarchy, DrawAll n Thus, we can reuse Shape and DrawAll enables the Open/ closed principle n Software entities (classes, modules) should be open for extension but closed for modification n Credited to Bertrand Meyer Fall 17 CSCI 2600, A Milanova 8 n Science of software design teaches Design Patterns n Design patterns promote design for extensibility and reuse n Nearly all design patterns make use of subtype polymorphism Fall 17 CSCI 2600, A Milanova 9 Outline 10 What is True Subtyping? n Subtyping, conceptually n B is subtype of A means every B is an A n In other words, a B object can be substituted where an A object is expected n The notion of true subtyping connects subtyping in the real world with Java subtyping Examples of Subtypes n Subset subtypes n int is a subtype of real n range [0..10] is a subtype of range [-10 10] n Other subtypes n Every book is a library item n Every DVD is a library item n Every triangle is a shape Shape n Etc. Book Item DVD Fall 17 CSCI 2600, A Milanova 11 Triangle Rombus Fall 17 CSCI 2600, A Milanova 12 2

True Subtypes are Substitutable n Subtypes are substitutable for supertypes n Instances of subtypes won t surprise client by requiring more than the supertype n Instances of subtypes won t surprise client by returning less than its supertype is realized through subclassing n Java subtype is not the same as true subtype! 13 Subtyping and Subclassing n Subtyping and substitutability --- specification notions n 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 n Subclassing and inheritance --- implementation notions n B extends A, or B implements A n B is a Java subtype of A, but not necessarily a true subtype of A! 14 True Subtype Subclassing. Inheritance Makes it Easy to Add Functionality n We say that (class) B is a true subtype of A if B has a stronger specification than A n Heed when designing inheritance hierarchies! n Java subtypes that are not true subtypes are confusing and dangerous 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 Fall 17 CSCI 2600, A Milanova 15 Fall 17 CSCI 2600, A Milanova (slide due to Michael Ernst) 16 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; Fall 17 CSCI 2600, A Milanova (based on an example by Michael Ernst) 17 Subclassing n 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 Fall 17 CSCI 2600, A Milanova (based on slide by Michael Ernst) 18 3

Benefits of Subclassing n Don t repeat unchanged fields and methods n Simpler maintenance: fix bugs once n Differences are clear (not buried under mass of similarity!) n Modularity: can ignore private fields and methods of superclass n Can substitute new implementations where old one is expected (the benefit of subtype polymorphism) n Another example: Timestamp extends Date Fall 17 CSCI 2600, A Milanova (based on slide by Michael Ernst) 19 Subclassing Can Be Misused n Poor planning leads to muddled inheritance hierarchies. Requires careful planning n If a class is not a true subtype of its superclass, it can surprise client n If class depends on implementation details of superclass, changes in superclass can break subclass. Fragile base class problem Fall 17 CSCI 2600, A Milanova (based on a slide by Michael Ernst) 20 Classic Example of Subtyping vs. Subclassing: 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 { // returns: area of rectangle public int area(); Fall 17 CSCI 2600, A Milanova 21 class Square extends Rectangle { // requires: w = h Choice 1: // effects: this post.width=w,this post.height=w Choice 2: // effects: this post.width=s,this post.height=s Choice 3: public void setsize(int s); // throws: BadSizeException if w!= h Choice 4: Fall 17 CSCI 2600, A Milanova (based on a slide by Mike Ernst) 22 n Choice 1 is not good n It requires more! Clients of Rectangle are justified to have Rectangle r; r.setsize(5,4) n In formal terms: spec of Square s setsize is not stronger than spec of Rectangle s setsize n Because the precondition of Rectangle s setsize does not imply precondition of Square s setsize n Thus, a Square can t be substituted for a Rectangle Fall 17 CSCI 2600, A Milanova 23 n Choice 4? n It throws an exception that clients of Rectangle are not expecting and not handling n Thus, a Square can t be substituted for a Rectangle n Choice 3? n Clients of Rectangle can write r.setsize(5,4). Square works with r.setsize(5) n Choice 2? n Client: Rectangle r; r.setsize(5,4); assert(r.area()==20) n Again, Square surprises client with behavior that is different from Rectangle s 24 4

n Square is not a true subtype of Rectangle n Rectangles are expected to have height and width that can change independently n Squares violate that expectation. Surprise clients n Is Rectangle a true subtype of Square? n No. Squares are expected to have equal height and width. Rectangles violate this expectation n One solution: make them Shape unrelated Square Rectangle Fall 17 CSCI 2600, A Milanova 25 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); 26 Liskov Substitution Principle (LSP) n Due to Barbara Liskov, Turing Award 2008 n LSP: A subclass should be substitutable for superclass. I.e., every subclass should be a true subtype of its superclass n Ensure that B is a true subtype of A by reasoning at the specification level n B should not remove methods from A n For each B.m that substitutes A.m, B.m s spec is stronger than A.m s spec n Client: A a; a.m(int x,int y); Call a.m can 27 bind to B s m. B s m should not surprise client Exercise: Reason About Specs class Rectangle { class Square extends Rectangle { // requires: w = h Fall 17 CSCI 2600, A Milanova (based on a slide by Mike Ernst) 28 Summary So Far n Java subtypes (realized with extends, implements) must be true subtypes n Java subtypes that are not true subtypes are dangerous and confusing n When B is a Java subtype of A, ensure n B, does not remove methods from A n A substituting method B.m has stronger spec than method A.m which it substitutes n Guarantees substitutability 29 Outline of today s class Fall 17 CSCI 2600, A Milanova 30 5