SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin
Agenda – Lecture 5a Homework (4b) Responsibilities, an introduction Quiz 1 review, … OO Objects and variables. Inheritance, subtyping. 2/4/2019 SOEN 343, © P.Chalin,
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,
Responsibility Assignment GRASP – Larman. Responsibility-Drive Design (RDD) – Wirfs-Brock et. al. 2/4/2019 SOEN 343, © P.Chalin,
General Classification of Kinds of Responsibility To know. To do. To decide. 2/4/2019 SOEN 343, © P.Chalin,
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,
Responsibilities – A Boat Metaphor Kind of responsibility for: Captain To know? To do? To decide? 2/4/2019 SOEN 343, © P.Chalin,
Responsibilities – A Boat Metaphor Kind of responsibility for: Navigator. To know? To do? To decide? 2/4/2019 SOEN 343, © P.Chalin,
Responsibilities – A Boat Metaphor Kind of responsibility for: Compass. To know? To do? To decide? 2/4/2019 SOEN 343, © P.Chalin,
Quiz 1 Focus OO fundamentals. Basic UML. Basic GRASP (applied). 2/4/2019 SOEN 343, © P.Chalin,
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,
Quiz 1, Qn 1: Objects and Variables Some ‘theory’ … 2/4/2019 SOEN 343, © P.Chalin,
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,
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,
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,
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,
Stack and Heap Stack Heap i 2 b r ? [0,0] s t null “abc” 2/4/2019 SOEN 343, © P.Chalin,
Quiz 1, Qn 2: Type Hierarchy 2/4/2019 SOEN 343, © P.Chalin,
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,
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,
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,
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,
Type Hierarchy: Object, the root of all K B X 2/4/2019 SOEN 343, © P.Chalin,
Type Hierarchy: Supertypes, Subtypes Supertypes of A Object Subtypes of A A K B X 2/4/2019 SOEN 343, © P.Chalin,
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,
‘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,
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,
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,
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,
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,
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,
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,
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,
Question 3 a : A c : C b : B 1 : m1( ) 2 : m2 ( ) c 2/4/2019 SOEN 343, © P.Chalin,
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,
Question 4 Variant of Exercise Set 1, Question 3. 2/4/2019 SOEN 343, © P.Chalin,
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,
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,
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,
Question 5, GRASP: Polymorphism, … Solution: Exercise Set 3 Tutorial 3 2/4/2019 SOEN 343, © P.Chalin,
Computing Movie Charge 2/4/2019 SOEN 343, © P.Chalin,