CS 320 Introduction to Software Engineering Spring March 06, 2017

Similar documents
CS 520 Theory and Practice of Software Engineering Fall 2017

CS 520 Theory and Practice of Software Engineering Fall 2018

CS 320 Introduction to Software Engineering Spring March

CS 320 Introduction to Software Engineering Spring 2017

Software Engineering

Intro to: Design Principles

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

Inheritance and object compatibility

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

Liskov Substitution Principle

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Introduction to Object-Oriented Programming

Inheritance and Polymorphism

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

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

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

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Java Object Oriented Design. CSC207 Fall 2014

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

CS-202 Introduction to Object Oriented Programming

ITI Introduction to Computing II

CS 251 Intermediate Programming Inheritance

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

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

First IS-A Relationship: Inheritance

Lecture: Modular Design

ITI Introduction to Computing II

COURSE 2 DESIGN PATTERNS

Object Oriented Software Design - I

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Inheritance. Transitivity

Object Oriented Programming. Java-Lecture 11 Polymorphism

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

More Relationships Between Classes

Example: Count of Points

Object-oriented Programming. Object-oriented Programming

Programming in C# Inheritance and Polymorphism

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

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

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

The S.O.L.I.D. Principles. of Object Oriented Programming

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

CS 520 Theory and Practice of Software Engineering Fall 2018

Concepts of Programming Languages

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

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Inheritance and Substitution (Budd chapter 8, 10)

S.O.L.I.D: Software Engineering Principles

Example: Count of Points

CS111: PROGRAMMING LANGUAGE II

Programming II (CS300)

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Exercise: Singleton 1

CMSC 132: Object-Oriented Programming II

CSE 307: Principles of Programming Languages

Programming Languages 2nd edition Tucker and Noonan"

Open Closed Principle (OCP)

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

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

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

Conformance. Object-Oriented Programming Spring 2015

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Last class. -More on polymorphism -super -Introduction to interfaces

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

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

C12a: The Object Superclass and Selected Methods

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

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

Introduction to Object Oriented Programming. (Hebrew University, CS / Spring 2012 )

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Review sheet for Final Exam (List of objectives for this course)

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

The class Object. Lecture CS1122 Summer 2008

ECE 3574: Dynamic Polymorphism using Inheritance

Practice for Chapter 11

CS 520 Theory and Practice of Software Engineering Fall 2017

Principles of Software Construction: Objects, Design, and Concurrency. Part 1: Designing Classes. Design for Reuse School of Computer Science

OBJECT ORİENTATİON ENCAPSULATİON

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas

Reusing Classes. Hendrik Speleers

OO Design Principles

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

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

Goals of the Lecture OO Programming Principles

CSC207 Week 3. Larry Zhang

Programming II (CS300)

Chapter 5 Object-Oriented Programming

CSC324 Principles of Programming Languages

CSC207H: Software Design SOLID. CSC207 Winter 2018

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Principles of Software Construction: Objects, Design, and Concurrency

Software Engineering Design & Construction

Polymorphism: Inheritance Interfaces

IS-A / HAS-A. I IS-A Teacher and I HAS-A Laptop

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Transcription:

CS 320 Introduction to Software Engineering Spring 2017 March 06, 2017

Recap: types of Polymorphism

Recap: types of Polymorphism Ad-hoc polymorphism (e.g., operator overloading) a + b String vs. int, double, etc. Subtype polymorphism (e.g., method overriding) Object obj = ; tostring() can be overridden in subclasses obj.tostring(); and therefore provide a different behavior. Parametric polymorphism (e.g., Java generics) class LinkedList<E> { A LinkedList can store elements void add(e) { regardless of their type but still E get(int index) { provide full type safety.

Recap: inheritance of classes and interfaces SequentialList {abstract <<interface>> List <<interface>> Deque LinkedList

Recap: inheritance of classes and interfaces LinkedList extends SequentialList implements List, Deque SequentialList {abstract extends <<interface>> List implements implements <<interface>> Deque LinkedList

Recap: inheritance of classes and interfaces <<interface>> Iterable extends <<interface>> Collection extends SequentialList {abstract <<interface>> List <<interface>> Deque extends implements implements LinkedList

Today Software design principles The diamond of death Composition/aggregation over inheritance Information hiding (and encapsulation) Open/closed principle Liskov substitution principle

Today Software design principles The diamond of death Composition/aggregation over inheritance Information hiding (and encapsulation) Open/closed principle Liskov substitution principle

The diamond of death A + getnum():int A a = new D(); int num = a.getnum(); B + getnum():int C + getnum():int D

The diamond of death A + getnum():int A a = new D(); int num = a.getnum(); B + getnum():int C + getnum():int What version of getnum() to call? D

The diamond of death A + getnum():int A a = new D(); int num = a.getnum(); B + getnum():int C + getnum():int D Can you think of a particular method in Java for which this problem could arise (if Java would allow multiple inheritance)?

Classes, abstract classes, and interfaces MyClass MyAbstractClass {abstract <<interface>> MyInterface public class MyClass { public void op() { public abstract class MyAbstractClass { public abstract void op(); public interface MyInterface { public void op(); public int op2() { public int op2() { public int op2(); Recall how default methods (Java 8) fit into this spectrum?

Classes, abstract classes, and interfaces MyClass MyAbstractClass {abstract <<interface>> MyInterface public class MyClass { public void op() { public abstract class MyAbstractClass { public abstract void op(); public interface MyInterface { public void op(); public int op2() { public int op2() { public default int op2() { So, how does an interface with default methods differ from an abstract class?

Coding example: cs320/arraylist.java (v1,v2) Why does Java 8 allow default methods? Compile all files in cs320 what do you observe? Fix the compilation issue. Source code is available on the course web site.

Coding example: cs320/arraylist.java (v3) Why does Java 8 allow default methods? Compile all files in cs320 what do you observe? Fix the compilation issue. Pretend 10 years have passed Add a new method to the List interface (List.java): public int getnumelems() Compile all files in cs320 what do you observe? Source code is available on the course web site.

Coding example: cs320/arraylist.java (v4) Why does Java 8 allow default methods? Compile all files in cs320 what do you observe? Fix the compilation issue. Pretend 10 years have passed Add a new method to the List interface (List.java): public int getnumelems() Compile all files in cs320 what do you observe? Make getnumelems a default method in List.java public default int getnumelems() { Compile all files in cs320 what do you observe? Source code is available on the course web site.

Coding example: inheritance/pegasus.java Default methods in Java can cause the diamond of death! Remove the canfly() method in Pegasus.java Compile the code what do you observe? Source code is available on the course web site.

Coding example: inheritance/pegasus.java Animal + canfly():bool Bird + canfly():bool Horse + canfly():bool Pegasus

Coding example: inheritance/pegasus.java Default methods in Java can cause the diamond of death! Remove the canfly() method in Pegasus.java Compile the code what do you observe? public class Pegasus implements Horse, Bird { public boolean canfly() { return true; How can you resolve the conflict without hard-coding the return value in the canfly method?

Coding example: inheritance/pegasus.java Default methods in Java can cause the diamond of death! Remove the canfly() method in Pegasus.java Compile the code what do you observe? public class Pegasus implements Horse, Bird { public boolean canfly() { return Bird.super.canFly();

Today Software design principles The diamond of death Composition/aggregation over inheritance Information hiding (and encapsulation) Open/closed principle Liskov substitution principle

Design choice: inheritance or composition? LinkedList LinkedList Stack Stack public class Stack<E> extends LinkedList<E> { public class Stack<E> { private List<E> l = new LinkedList<>();

Design choice: inheritance or composition? LinkedList LinkedList Stack Stack public class Stack<E> extends LinkedList<E> { public class Stack<E> { private List<E> l = new LinkedList<>(); Hmm, both designs seem valid -- what are pros and cons?

Design choice: inheritance or composition? LinkedList LinkedList Stack Stack Pros No delegation methods required. Reuse of common state and behavior. Cons Exposure of all inherited methods (a client might rely on this particular superclass -> can t change it later). Changes in superclass are likely to break subclasses. Pros Highly flexible and configurable. No additional subclasses required for different has-a relationships. Cons All interface methods need to be implemented -> delegation methods required, even for code reuse. Composition/aggregation over inheritance allows more flexibility.

Today Software design principles The diamond of death Composition/aggregation over inheritance Information hiding (and encapsulation) Open/closed principle Liskov substitution principle

Information hiding MyClass + nelem : int + capacity : int + top : int + elems : int[] + canresize : bool + resize(s:int):void + push(e:int):void + capacityleft():int + getnumelem():int + pop():int + getelems():int[]

Information hiding MyClass + nelem : int + capacity : int + top : int + elems : int[] + canresize : bool public class MyClass { public int nelem; public int capacity; public int top; public int[] elems; public boolean canresize; + resize(s:int):void + push(e:int):void + capacityleft():int + getnumelem():int + pop():int + getelems():int[] public void resize(int s){ public void push(int e){ public int capacityleft(){ public int getnumelem(){ public int pop(){ public int[] getelems(){

Information hiding MyClass + nelem : int + capacity : int + top : int + elems : int[] + canresize : bool public class MyClass { public int nelem; public int capacity; public int top; public int[] elems; public boolean canresize; + resize(s:int):void + push(e:int):void + capacityleft():int + getnumelem():int + pop():int + getelems():int[] public void resize(int s){ public void push(int e){ public int capacityleft(){ public int getnumelem(){ public int pop(){ public int[] getelems(){ What does MyClass do?

Information hiding Stack + nelem : int + capacity : int + top : int + elems : int[] + canresize : bool public class Stack { public int nelem; public int capacity; public int top; public int[] elems; public boolean canresize; + resize(s:int):void + push(e:int):void + capacityleft():int + getnumelem():int + pop():int + getelems():int[] public void resize(int s){ public void push(int e){ public int capacityleft(){ public int getnumelem(){ public int pop(){ public int[] getelems(){ Anything that could be improved in this implementation?

Information hiding Stack + nelem : int + capacity : int + top : int + elems : int[] + canresize : bool + resize(s:int):void + push(e:int):void + capacityleft():int + getnumelem():int + pop():int + getelems():int[] Stack - elems : int[] + push(e:int):void + pop():int Information hiding: Reveal as little information about internals as possible. Segregate public interface and implementation details. Reduces complexity.

Information hiding vs. visibility Public??? Private

Information hiding vs. visibility Public??? Protected, package-private, or friend-accessible (C++). Not part of the public API. Implementation detail that a subclass/friend may rely on. Private

Today Software design principles The diamond of death Composition/aggregation over inheritance Information hiding (and encapsulation) Open/closed principle Liskov substitution principle

Design principles: open/closed principle Software entities (classes, components, etc.) should be: open for extensions closed for modifications public static void draw(object f) { if (f instanceof Square) { drawsquare((square) f) else if (f instanceof Circle) { drawcircle((circle) f); else { Good or bad design? Square + drawsquare() Circle + drawcircle()

Design principles: open/closed principle Software entities (classes, components, etc.) should be: open for extensions closed for modifications public static void draw(object f) { if (f instanceof Square) { drawsquare((square) f) else if (f instanceof Circle) { drawcircle((circle) f); else { Violates the open/closed principle! Square + drawsquare() Circle + drawcircle()

Design principles: open/closed principle Software entities (classes, components, etc.) should be: open for extensions closed for modifications public static void draw(object f) { if (f instanceof Figure) { f.draw(); else { <<interface>> Shape + draw() public static void draw(figure f) { f.draw(); Square Circle

Today Software design principles The diamond of death Composition/aggregation over inheritance Information hiding (and encapsulation) Open/closed principle Liskov substitution principle

Design principles: Liskov substitution principle Motivating example We know that a square is a special kind of a rectangle. So, which of the following OO designs makes sense? Square Rectangle Rectangle Square

Design principles: Liskov substitution principle Subtype requirement Let object x be of type T1 and object y be of type T2. Further, let T2 be a subtype of T1 (T2 <: T1). Any provable property about objects of type T1 should be true for objects of type T2. Rectangle Rectangle + width :int + height:int + setwidth(w:int) + setheight(h:int) + getarea():int Square Is the subtype requirement fulfilled?

Design principles: Liskov substitution principle Subtype requirement Let object x be of type T1 and object y be of type T2. Further, let T2 be a subtype of T1 (T2 <: T1). Any provable property about objects of type T1 should be true for objects of type T2. Rectangle + width :int + height:int + setwidth(w:int) + setheight(h:int) + getarea():int Rectangle r = new Rectangle(2,2); int A = r.getarea(); int w = r.getwidth(); r.setwidth(w * 2); assertequals(a * 2, r.getarea()); Rectangle Square

Design principles: Liskov substitution principle Subtype requirement Let object x be of type T1 and object y be of type T2. Further, let T2 be a subtype of T1 (T2 <: T1). Any provable property about objects of type T1 should be true for objects of type T2. Rectangle + width :int + height:int + setwidth(w:int) + setheight(h:int) + getarea():int Rectangle r = new Rectangle(2,2); new Square(2); int A = r.getarea(); int w = r.getwidth(); r.setwidth(w * 2); assertequals(a * 2, r.getarea()); Rectangle Square

Design principles: Liskov substitution principle Subtype requirement Let object x be of type T1 and object y be of type T2. Further, let T2 be a subtype of T1 (T2 <: T1). Any provable property about objects of type T1 should be true for objects of type T2. Rectangle + width :int + height:int + setwidth(w:int) + setheight(h:int) + getarea():int Rectangle r = new Rectangle(2,2); new Square(2); int A = r.getarea(); int w = r.getwidth(); r.setwidth(w * 2); assertequals(a * 2, r.getarea()); Violates the Liskov substitution principle! Rectangle Square

Design principles: Liskov substitution principle Subtype requirement Let object x be of type T1 and object y be of type T2. Further, let T2 be a subtype of T1 (T2 <: T1). Any provable property about objects of type T1 should be true for objects of type T2. Rectangle + width :int + height:int <<interface>> Shape + setwidth(w:int) + setheight(h:int) + getarea():int Rectangle Square

Summary Software design principles The diamond of death Composition/aggregation over inheritance Information hiding (and encapsulation) Open/closed principle Liskov substitution principle