SOEN 343 Software Design Computer Science and Software

  • Slides: 41
Download presentation
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004

SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin SOEN 343, © P. Chalin SOEN 344, © P. Chalin

Agenda – Lecture 5 a • • Homework (4 b) Responsibilities, an introduction Quiz

Agenda – Lecture 5 a • • Homework (4 b) Responsibilities, an introduction Quiz 1 review, … OO – Objects and variables. – Inheritance, subtyping. 9/7/2021 SOEN 343, © P. Chalin, 2

Homework was … • Planned (e. g. MDD) vs. Evolutionary (e. g. XP) •

Homework was … • Planned (e. g. MDD) vs. Evolutionary (e. g. XP) • How does each approach address: – No doc. produced. – Increased gap: design vs. implementation. – Add-on syndrome. 9/7/2021 SOEN 343, © P. Chalin, 3

Responsibility Assignment • GRASP – Larman. • Responsibility-Drive Design (RDD) – Wirfs-Brock et. al.

Responsibility Assignment • GRASP – Larman. • Responsibility-Drive Design (RDD) – Wirfs-Brock et. al. 9/7/2021 SOEN 343, © P. Chalin, 4

General Classification of Kinds of Responsibility – To know. – To do. – To

General Classification of Kinds of Responsibility – To know. – To do. – To decide. 9/7/2021 SOEN 343, © P. Chalin, 5

Responsibilities – A Boat Metaphor • What kind of responsibilities do each of the

Responsibilities – A Boat Metaphor • What kind of responsibilities do each of the following “objects” have: … – To know. – To do. – To decide. 9/7/2021 SOEN 343, © P. Chalin, 6

Responsibilities – A Boat Metaphor Kind of responsibility for: • Captain – To know?

Responsibilities – A Boat Metaphor Kind of responsibility for: • Captain – To know? – To do? – To decide? 9/7/2021 SOEN 343, © P. Chalin, 7

Responsibilities – A Boat Metaphor Kind of responsibility for: • Navigator. – To know?

Responsibilities – A Boat Metaphor Kind of responsibility for: • Navigator. – To know? – To do? – To decide? 9/7/2021 SOEN 343, © P. Chalin, 8

Responsibilities – A Boat Metaphor Kind of responsibility for: • Compass. – To know?

Responsibilities – A Boat Metaphor Kind of responsibility for: • Compass. – To know? – To do? – To decide? 9/7/2021 SOEN 343, © P. Chalin, 9

Quiz 1 Focus • OO fundamentals. • Basic UML. • Basic GRASP (applied). 9/7/2021

Quiz 1 Focus • OO fundamentals. • Basic UML. • Basic GRASP (applied). 9/7/2021 SOEN 343, © P. Chalin, 10

Quiz 1 Review • You will be given explanation, not necessarily answers. • If

Quiz 1 Review • You will be given explanation, not necessarily answers. • If you need further clarification: – My office hours. – Ask during tutorial. 9/7/2021 SOEN 343, © P. Chalin, 11

Quiz 1, Qn 1: Objects and Variables • Some ‘theory’ … 9/7/2021 SOEN 343,

Quiz 1, Qn 1: Objects and Variables • Some ‘theory’ … 9/7/2021 SOEN 343, © P. Chalin, 12

Object and Variables: Run-time Initialization • Local variables: – Are not implicitly initialized. –

Object and Variables: Run-time Initialization • Local variables: – Are not implicitly initialized. – Must be explicitly initialized. • Object fields always initialized by default to – Integral types (including char): 0. – Floating point types: 0. 0. – Reference types: null. 9/7/2021 SOEN 343, © P. Chalin, 13

Object Creation and Variable Declarations • Basic declaration (no object creation): Animal a; •

Object Creation and Variable Declarations • Basic declaration (no object creation): Animal a; • null initialized declaration (no object creation): Animal a = null; • Only using new will create (instantiate) objects a = new Duck(); 9/7/2021 SOEN 343, © P. Chalin, 14

Objects and Variables: Key Points What you should know: • Run-time memory model –

Objects and Variables: Key Points What you should know: • Run-time memory model – Has two parts: Stack, Heap. – You should know what gets allocated where. • Object creation and variable declarations. – Declarations do not cause objects to be created. – Know the role of null. – Object creation / instantiation: via new. 9/7/2021 SOEN 343, © P. Chalin, 15

Stack and Heap: Quiz Question • Given the following declarations int[] String i =

Stack and Heap: Quiz Question • Given the following declarations int[] String i = b = r; s = t = 2; new int[2]; “abc”; null; • What will be the state of the stack and heap after they have all been processed? 9/7/2021 SOEN 343, © P. Chalin, 16

Stack and Heap Stack i 2 [0, 0] b r ? s t 9/7/2021

Stack and Heap Stack i 2 [0, 0] b r ? s t 9/7/2021 Heap “abc” null SOEN 343, © P. Chalin, 17

Quiz 1, Qn 2: Type Hierarchy 9/7/2021 SOEN 343, © P. Chalin, 18

Quiz 1, Qn 2: Type Hierarchy 9/7/2021 SOEN 343, © P. Chalin, 18

Disjointedness of Primitive and Reference Types • You cannot assign a primitive type to

Disjointedness of Primitive and Reference Types • You cannot assign a primitive type to a variable of a reference type. • You cannot assign a reference to a variable of a primitive type. • The remaining few slides discuss only reference types. 9/7/2021 SOEN 343, © P. Chalin, 19

Type Hierarchy (Lect. 1 b) • Every class is a subclass of Object. •

Type Hierarchy (Lect. 1 b) • Every class is a subclass of Object. • If S is a subclass of T then we can use an instance of S where ever a T is expected: T t = new S(); Object o = new S(); // Do not do this (it is wasteful): Object o = new String(“abc”); 9/7/2021 SOEN 343, © P. Chalin, 20

Each Reference Variable: Two Types Each variable of a reference type has • Declared

Each Reference Variable: Two Types Each variable of a reference type has • Declared type – fixed. • Run-time type – can change at run-time. • Synonyms: – Declared type: apparent type. – Run-time type: dynamic, or actual type. 9/7/2021 SOEN 343, © P. Chalin, 21

Each Variable: Two Types, Example Object m = new Movie(); • m has two

Each Variable: Two Types, Example Object m = new Movie(); • m has two types: – Declared type: Object. – Run-time type: Movie. 9/7/2021 SOEN 343, © P. Chalin, 22

Type Hierarchy: Object, the root of all Object A K B X 9/7/2021 SOEN

Type Hierarchy: Object, the root of all Object A K B X 9/7/2021 SOEN 343, © P. Chalin, 23

Type Hierarchy: Supertypes, Subtypes Object Supertypes of A A K B Subtypes of A

Type Hierarchy: Supertypes, Subtypes Object Supertypes of A A K B Subtypes of A X 9/7/2021 SOEN 343, © P. Chalin, 24

Unrelated Sub-hierarchies are Not Compatible (for assignment, cast). Object • Cannot – Assign, or

Unrelated Sub-hierarchies are Not Compatible (for assignment, cast). Object • Cannot – Assign, or – Cast A to K A K B X 9/7/2021 SOEN 343, © P. Chalin, 25

‘Declared Type’ Fixes Bounds • Declared type – fixed, e. g. A. • Run-time

‘Declared Type’ Fixes Bounds • Declared type – fixed, e. g. A. • Run-time type – can change at run-time. . . – … within bounds of declared type subhierarchy. A B X 9/7/2021 SOEN 343, © P. Chalin, 26

Type Checking: Assignment • Always done relative to declared type. A a = some-expression-of-type-X

Type Checking: Assignment • Always done relative to declared type. A a = some-expression-of-type-X • Legal? (or will cause a compile-time error? ) • Assignment is legal iff A X is – A, B – Subtype of A. X 9/7/2021 SOEN 343, © P. Chalin, 27

Type Casting and Type Checking I A a = (X) some-expression-of-type-K; • Legal? (will

Type Casting and Type Checking I A a = (X) some-expression-of-type-K; • Legal? (will cause a compile-time error? ) • Same answer as given on previous slide because this is just a special case of the previous slide. 9/7/2021 SOEN 343, © P. Chalin, 28

Type Casting and Type Checking II A a = (X) some-expression-of-type-K; • Legal? (will

Type Casting and Type Checking II A a = (X) some-expression-of-type-K; • Legal? (will cause a compile-time error? ) • Error unless X and K are related (super- or subtype). X K … Z 9/7/2021 SOEN 343, © P. Chalin, 29

Type Casting and Run-time Checks? I A a = (X) some-expression-of-type-K; • If X

Type Casting and Run-time Checks? I A a = (X) some-expression-of-type-K; • If X and K are related then: – X is a supertype of K: nothing is needed. X … K 9/7/2021 SOEN 343, © P. Chalin, 30

Type Casting and Run-time Checks? II A a = (X) some-expression-of-type-K; • If X

Type Casting and Run-time Checks? II A a = (X) some-expression-of-type-K; • If X and K are related then: – X is a subtype of K: a run-time check is needed. K … X 9/7/2021 SOEN 343, © P. Chalin, 31

Question 2 (a)-(c), Another Try Find the answer (but also tell me why): (a)

Question 2 (a)-(c), Another Try Find the answer (but also tell me why): (a) Object o = 1; C/R /N (b) Object o = s; C/R/N (c) Object o = 9/7/2021 SOEN 343, © P. Chalin, 32

Question 2 (d)-(f), Another Try (d) String t = x; C/R /N (e) String

Question 2 (d)-(f), Another Try (d) String t = x; C/R /N (e) String t = (String)x; C/R/N (f) String t = (Object)s; C/R/N 9/7/2021 SOEN 343, © P. Chalin, 33

Question 3 a: A b: B c: C 1 : m 1( ) c

Question 3 a: A b: B c: C 1 : m 1( ) c 2 : m 2 ( ) 9/7/2021 SOEN 343, © P. Chalin, 34

Question 3, Code – possible answer class A { B b; public void m()

Question 3, Code – possible answer class A { B b; public void m() { C c = b. m 1(); c. m 2(); } } class B { C c; . . . public C m 1() {. . . ; return c; } } class C { public void m 2() {. . . } } 9/7/2021 SOEN 343, © P. Chalin, 35

Question 4 • Variant of Exercise Set 1, Question 3. 9/7/2021 SOEN 343, ©

Question 4 • Variant of Exercise Set 1, Question 3. 9/7/2021 SOEN 343, © P. Chalin, 36

Dynamic Dispatching … • Run-time type is only of concern when resolving non-static methods.

Dynamic Dispatching … • Run-time type is only of concern when resolving non-static methods. • Never for fields. • Not for static methods. 9/7/2021 SOEN 343, © P. Chalin, 37

Overridden Non-static Field • There are two fields (one in P, one in C).

Overridden Non-static Field • There are two fields (one in P, one in C). • Moral: do not do this! P f : int 9/7/2021 C f : int SOEN 343, © P. Chalin, 38

Static Methods • No overriding • P. m(), C. m() are distinct methods, both

Static Methods • No overriding • P. m(), C. m() are distinct methods, both accessible. C P int m() 9/7/2021 int m() SOEN 343, © P. Chalin, 39

Question 5, GRASP: Polymorphism, … • Solution: – Exercise Set 3 – Tutorial 3

Question 5, GRASP: Polymorphism, … • Solution: – Exercise Set 3 – Tutorial 3 9/7/2021 SOEN 343, © P. Chalin, 40

Computing Movie Charge 9/7/2021 SOEN 343, © P. Chalin, 41

Computing Movie Charge 9/7/2021 SOEN 343, © P. Chalin, 41