SOEN 343 Software Design - PowerPoint PPT Presentation

About This Presentation
Title:

SOEN 343 Software Design

Description:

Unchecked. Checked. Must be listed in method declaration. ... Unchecked. No such restrictions. Exception. Error. Throwable. RuntimeException. SampleChecked ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 33
Provided by: ENCS
Category:

less

Transcript and Presenter's Notes

Title: SOEN 343 Software Design


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

2
Announcements
  • Tutorials start this Friday
  • Tutorial/Lab location
  • Still TBD watch web page
  • Additional tutorial slot
  • Fridays 1645 to 1735 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).
  • public class HelloWorld
  •   public static void 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 int2
  • 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
Type Hierarchy Object, root of all
17
Type Hierarchy Supertypes, Subtypes
18
Unrelated Sub-hierarchies are Not Compatible (for
assignment, cast).
  • 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 ...
  • within bounds of declared type subhierarchy.

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.

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?
  • class Animal
  • protected int numberOfLegs
  • public int getNumberOfLegs()
  • return numberOfLegs
  • class Duck extends 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
Overridden Non-static Field
  • There are two fields (one in P, one in C).
  • Moral do not do this!

28
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
Exception
Error
RuntimeException
SampleChecked
32
Exceptions
  • Both
  • methods
  • constructors
  • can throw exceptions.
Write a Comment
User Comments (0)
About PowerShow.com