1 / 32

SOEN 343 Software Design

SOEN 343 Software Design. Section H Fall 2006 Dr Greg Butler http://www.cs.concordia.ca/~gregb/home/soen343h-f06.html. 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. OO Review.

bdupree
Download Presentation

SOEN 343 Software Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SOEN 343Software 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! • Illustrates: • How to write the main method to be called when a program is invoked. • Output (console). publicclass HelloWorld { publicstaticvoid main(String[] args) {      System.out.println("HelloWorld!");   } }

  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 Heap Stack i b r s t 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: • Declared type: static type, or apparent type. • 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. Object A B X K Type Hierarchy: Object, root of all

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

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

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

  20. A B 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 X is • A, • Subtype of A.

  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 Issues? classAnimal { protected int numberOfLegs; public int getNumberOfLegs() { return numberOfLegs; } } classDuckextends Animal { public Duck() { numberOfLegs = 2; } … } 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. P C f : int f : int Overridden Non-static Field • There are two fields (one in P, one in C). • Moral: do not do this!

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

  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. Throwable Error Exception RuntimeException SampleChecked

  32. Exceptions • Both • methods • constructors can throw exceptions.

More Related