Outline Object Oriented Programming Autumn 2003 2 Course goals Software design vs hacking Abstractions vs language (syntax) Java used to illustrate concepts NOT a course about Java Prerequisites knowledge of C, C++, or similar some programming experience Staff Lecturer: Timour Katchaounov (Timour.Katchaounov@it.uu.se) Assistants (seminars, labs): Jesper Wilhelmsson (Jesper.Wilhelmsson@it.uu.se) Ruslan Fomkin (Ruslan.Fomkin@it.uu.se) E-mail subject: OOP-HT03: xxx 3 4 Course resources All course information (schedule, slides, etc.): http://www.csd.uu.se/datalogi/cmtrl/oop/ht03/ Course management system: Everyone should register Accessible from university network http://udblserv.it.uu.se:8080/oop_ht03 Assignments Voluntary Java assignment After the lab assumed to know Java Pasture simulation Four assignments - 4 points Extra fifth assignment - 5 points 2 students per group 5 6
Literature Course: Barbara Liskov: Program Development in Java. Java: Bruce Eckel: Thinking in Java www.bruceeckel.com Java Language/API docs: java.sun.com developer.java.sun.com java.sun.com/docs/books/tutorial Outline 7 8 Software design Real systems are big Apache: ~50000 lines Communicator 4: ~3 000 000 lines Requires many people to cooperate Hard to understand, develop, maintain Need for methods to handle complexity => Decomposition and abstraction Decomposition Decompose problems into sub-problems Same level of detail Independent Combined solve the original problem Break up a system into modules 9 10 Abstraction Ignore details => focus on important properties Simplify the problem Many-to-one mapping Examples: sets, numbers, humans Design: abstract -> decompose -> abstract -> Abstraction methods Abstraction by parameterization ignore identity, focus on common properties module reusability Abstraction by specification ignore implementation, focus on function Module independence 11 12
Types of abstractions (1) Procedural abstractions abstract some operation Introduce new operations Data abstractions Sets of similar objects and their operations Operations are related Introduce new data types Types of abstractions (2) Iteration abstractions Ignore item storage & retrieval details Type hierarchies Group similar/related abstractions Allow to ignore differences Subtypes/supertypes 13 14 Types of abstractions (3) Combined use of parameterization and specification Abstraction is language independent Any programming language can be used Object-oriented languages make things easier The data abstraction is central for OO programming and design 15 Outline 16 Java overview Reference implementation by SUN Platform independent Compiled into bytecode (kind of assembler) Programs run on a virtual machine JVM Automatic memory management - garbage collection 17 OO abstractions in Java Specification => interfaces, javadoc, assertions Parameterization => methods, classes Data abstractions => classes & objects Procedural abstractions => methods Iteration abstractions => loops (for, while, do), iterators Type hierarchies => class inheritance 18
Java programs Consist of classes and/or an interfaces Collections of procedures Data abstractions No global variables, no free functions Each class in its own file Interface and implementation in one file (no.h files) Classes Consist of fields and methods public class Square extends Shape { private float xcoordinate; public void rotate(float degree) { 19 20 Java Types Classes - system and user defined Primitive types - system defined: byte, short, int, long, (8,16,32,64 bit), boolean, char (16 bit), float, double for performance Can be wrapped in classes Arrays: Are objects <type>[] <name>; String names[]; Can t grow Objects and variables Data accessed through variables Contain objects (on heap) or values (on stack) Primitive and reference variables Object creation - the new operator Equality: == and equals Object identity Mutability and sharing Array objects 21 22 Class fields Describe state of objects and classes <visibility> <type> <name> = <value>; public float xcoordinate = 74.3; Accessed through objects someobject.fieldname Static fileds - value shared by all class objects static <visibility> <type> <name> = <value>; Accessed through class: someclass.fieldname 23 Methods Method declaration and creation <visibility> <return type> <name>(formal params) { body; Call semantics expr.somemethod(param1, ); Class vs instance methods Constructor methods Initialize objects Used through new operator 24
Packages Group related classes and interfaces Provide for modularity Class/interface visibility Fully qualified names Known syntax for, while, do switch No goto No pointers 25 26 Java applications Console applications GUI applications Standalone vs. applets Java applications: the main method Entry point to run applications Static public main(string[] args) { One per class testing alternative program versions 27 28 Java applications: minimal Java program class hello { public static void main (String[] args) { System.out.println( Hello world! ); System.exit(0); 29