320 likes | 335 Views
Learn Java basics, OO programming principles, object creation, and stack/heap memory allocation. Understand variable types, type hierarchy, type casting, and polymorphism.
E N D
SOEN 343Software 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 OO programming • Java: class, interface, ‘static’ • Static and dynamic typing • Polymorphism, delegation • exceptions OO development using UML models
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);
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();
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!"); } }
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.
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();
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.
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?
Stack and Heap Heap Stack i b r s t 2 ? null [0,0] “abc”
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.
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”);
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.
Each Variable: Two Types, Example Object m = new Movie(); • m has two types: • Declared type: Object. • Run-time type: Movie.
Object A B X K Type Hierarchy: Object, root of all
Supertypes of A Object A B X K Subtypes of A Type Hierarchy: Supertypes, Subtypes
Object A B X K Unrelated Sub-hierarchies are Not Compatible (for assignment, cast). • Cannot • Assign, or • Cast A to K
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.
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.
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.
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?
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
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.
Number of Legs, Another Solution Issues? classAnimal { protected int numberOfLegs; public int getNumberOfLegs() { return numberOfLegs; } } classDuckextends Animal { public Duck() { numberOfLegs = 2; } … } Best solution?
Dynamic Dispatching … • Run-time type is only of concern when resolving non-static methods. • Never for fields. • Not for static methods.
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!
P C int m() int m() Static Methods • No overriding • P.m(), C.m() are distinct methods, both accessible.
Exceptions, An Example: Throwing public void m(String s) throws NullPointerException { if(s == null) { throw new NullPointerException(); } ... }
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 } }
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
Exceptions • Both • methods • constructors can throw exceptions.