Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin."— Presentation transcript:

1 SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin

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

3 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. 2/4/2019 SOEN 343, © P.Chalin,

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

5 General Classification of Kinds of Responsibility
To know. To do. To decide. 2/4/2019 SOEN 343, © P.Chalin,

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

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

8 Responsibilities – A Boat Metaphor
Kind of responsibility for: Navigator. To know? To do? To decide? 2/4/2019 SOEN 343, © P.Chalin,

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

10 Quiz 1 Focus OO fundamentals. Basic UML. Basic GRASP (applied).
2/4/2019 SOEN 343, © P.Chalin,

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

12 Quiz 1, Qn 1: Objects and Variables
Some ‘theory’ … 2/4/2019 SOEN 343, © P.Chalin,

13 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. 2/4/2019 SOEN 343, © P.Chalin,

14 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(); 2/4/2019 SOEN 343, © P.Chalin,

15 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. 2/4/2019 SOEN 343, © P.Chalin,

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

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

18 Quiz 1, Qn 2: Type Hierarchy
2/4/2019 SOEN 343, © P.Chalin,

19 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. 2/4/2019 SOEN 343, © P.Chalin,

20 Type Hierarchy (Lect. 1b)
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”); 2/4/2019 SOEN 343, © P.Chalin,

21 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. 2/4/2019 SOEN 343, © P.Chalin,

22 Each Variable: Two Types, Example
Object m = new Movie(); m has two types: Declared type: Object. Run-time type: Movie. 2/4/2019 SOEN 343, © P.Chalin,

23 Type Hierarchy: Object, the root of all
K B X 2/4/2019 SOEN 343, © P.Chalin,

24 Type Hierarchy: Supertypes, Subtypes
Supertypes of A Object Subtypes of A A K B X 2/4/2019 SOEN 343, © P.Chalin,

25 Unrelated Sub-hierarchies are Not Compatible (for assignment, cast).
Object Cannot Assign, or Cast A to K A B X K 2/4/2019 SOEN 343, © P.Chalin,

26 ‘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 2/4/2019 SOEN 343, © P.Chalin,

27 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 X is A, Subtype of A. A B X 2/4/2019 SOEN 343, © P.Chalin,

28 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. 2/4/2019 SOEN 343, © P.Chalin,

29 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 sub-type). X Z K 2/4/2019 SOEN 343, © P.Chalin,

30 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 2/4/2019 SOEN 343, © P.Chalin,

31 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 2/4/2019 SOEN 343, © P.Chalin,

32 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 = (Object) s; C / R / N 2/4/2019 SOEN 343, © P.Chalin,

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

34 Question 3 a : A c : C b : B 1 : m1( ) 2 : m2 ( ) c
2/4/2019 SOEN 343, © P.Chalin,

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

36 Question 4 Variant of Exercise Set 1, Question 3.
2/4/2019 SOEN 343, © P.Chalin,

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

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

39 Static Methods No overriding
P.m(), C.m() are distinct methods, both accessible. P int m() C int m() 2/4/2019 SOEN 343, © P.Chalin,

40 Question 5, GRASP: Polymorphism, …
Solution: Exercise Set 3 Tutorial 3 2/4/2019 SOEN 343, © P.Chalin,

41 Computing Movie Charge
2/4/2019 SOEN 343, © P.Chalin,


Download ppt "SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin."

Similar presentations


Ads by Google