Presentation is loading. Please wait.

Presentation is loading. Please wait.

SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler

Similar presentations


Presentation on theme: "SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler"— Presentation transcript:

1 SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler http://www.cs.concordia.ca/~gregb/home/soen343h-f06.html

2 Announcements Tutorials start this Friday Tutorial/Lab location Still TBD – watch web page Additional tutorial slot Fridays 16:45 to 17:35 in H-929

3 OO Review OO programming Java: class, interface, ‘static’ Static and dynamic typing Polymorphism, delegation exceptions OO development using UML models

4 Java – some details to be read on your own. Primitive types: include –Floating point : float, double. –Integral : byte,short,int,long,char. –boolean, is not assignment compatible with integral types; explicit conversion required. boolean b = (i == 0 ? false : true); Class types for “boxing”: –Float, Double, Byte, … Boolean. –E.g. Boolean o = new Boolean(b);

5 java.util.Vector Vector class –Implements a growable array of objects. Example of use String s = …; Vector v = new Vector(); v.add(s); // adds to end String t = v.elementAt(0); v.remove(s); int i = v.size();

6 Java Basics – Hello World! public class HelloWorld { public static void main(String[] args) { System.out.println("HelloWorld!"); } Illustrates: –How to write the main method to be called when a program is invoked. –Output (console).

7 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.

8 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 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.

10 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?

11 Stack and Heap Stack Heap ibrstibrst 2 ? null [0,0] “abc”

12 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.

13 Type Hierarchy 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”);

14 Each Reference Variable: Two Types Each variable of a reference type has Declared type – fixed. Run-time type – can change at run-time. Synonyms: –D–Declared type: static type, or apparent type. –R–Run-time type: dynamic, or actual type.

15 Each Variable: Two Types, Example Object m = new Movie(); m has two types: –Declared type: Object. –Run-time type: Movie.

16 Type Hierarchy: Object, root of all Object A B X K

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

18 Unrelated Sub-hierarchies are Not Compatible (for assignment, cast). Object A B X K Cannot –Assign, or –Cast A to K

19 ‘Declared Type’ Fixes Bounds Declared type – fixed, e.g. A. Run-time type – can change at run-time... –w–within bounds of declared type subhierarchy. A B X

20 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

21 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.

22 OO Basics: Static vs. Dynamic Typing Why not declare all fields or variables of reference types as –Object –void * (C++) What would we gain? What would we loose?

23 Type Casting (in Statically Typed OO Languages) Purpose of Casting –Inform compiler of (assumed) subtype of an object. –Compiler can then perform better type checking. Type cast –Like an assertion, it may fail; e.g. Object i = new Integer(0); String s = (String) i; //  ClassCastException

24 Polymorphism “poly” – many “morphism” – forms How does the meaning of this term apply to OO? –Run-time type of a given expression can vary. Different types: our concern –Subtype polymorphism.

25 Number of Legs, Another Solution class Animal { protected int numberOfLegs; public int getNumberOfLegs() { return numberOfLegs; } } class Duck extends Animal { public Duck() { numberOfLegs = 2; } … } Issues? Best solution?

26 Dynamic Dispatching … Run-time type is only of concern when resolving non-static methods. Never for fields. Not for static methods.

27 Overridden Non-static Field There are two fields (one in P, one in C). Moral: do not do this! P f : int C

28 Static Methods No overriding P.m(), C.m() are distinct methods, both accessible. P int m() C

29 Exceptions, An Example: Throwing public void m(String s) throws NullPointerException { if(s == null) { throw new NullPointerException(); }... }

30 Exceptions, An Example: Catching public void m2(…) { String t =...; try { m(t); } catch( NullPointerException e) { // t was null } finally { // code to exec no matter what }

31 Exception Class Hierarchy Two types of exceptions –Checked –Unchecked Checked –Must be listed in method declaration. –Must be caught – or run-time error is reported. Unchecked –No such restrictions. ExceptionError Throwable RuntimeExceptionSampleChecked

32 Exceptions Both –methods –constructors can throw exceptions.


Download ppt "SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler"

Similar presentations


Ads by Google