Object Oriented Programming
Autumn 2003
2
Outline
Course organizationIntroductionJava overview
3
Course goals
Software design vs hackingAbstractions vs language (syntax)Java used to illustrate conceptsNOT a course about JavaPrerequisites
knowledge of C, C++, or similarsome programming experience
4
Staff
Lecturer: Timour Katchaounov([email protected])
Assistants (seminars, labs):Jesper Wilhelmsson([email protected])
Ruslan Fomkin([email protected])
E-mail subject: “OOP-HT03: xxx”
5
Course resourcesAll course information (schedule, slides, etc.):http://www.csd.uu.se/datalogi/cmtrl/oop/ht03/
Course management system:Everyone should registerAccessible from university networkhttp://udblserv.it.uu.se:8080/oop_ht03
6
Assignments
Voluntary Java assignmentAfter the lab assumed to ‘know’ Java
Pasture simulationFour assignments - 4 pointsExtra fifth assignment - 5 points
2 students per group
7
LiteratureCourse:Barbara Liskov: Program Development in Java.
Java:Bruce Eckel: Thinking in Javawww.bruceeckel.comJava Language/API docs:
java.sun.comdeveloper.java.sun.comjava.sun.com/docs/books/tutorial
8
Outline
Course organizationIntroductionJava overview
9
Software design
Real systems are bigApache: ~50000 linesCommunicator 4: ~3 000 000 lines
Requires many people to cooperateHard to understand, develop, maintainNeed for methods to handle complexity
=> Decomposition and abstraction
10
Decomposition
Decompose problems into sub-problemsSame level of detailIndependentCombined solve the original problem
Break up a system into modules
11
Abstraction
Ignore details => focus on important propertiesSimplify the problemMany-to-one mappingExamples: sets, numbers, humansDesign:abstract -> decompose -> abstract -> …
12
Abstraction methods
Abstraction by parameterizationignore identity, focus on common propertiesmodule reusability
Abstraction by specificationignore implementation, focus on functionModule independence
13
Types of abstractions (1)
Procedural abstractionsabstract some operationIntroduce new operations
Data abstractionsSets of ‘similar’ objects and their operationsOperations are relatedIntroduce new data types
14
Types of abstractions (2)
Iteration abstractionsIgnore item storage & retrieval details
Type hierarchiesGroup similar/related abstractionsAllow to ignore differencesSubtypes/supertypes
15
Types of abstractions (3)Combined use of parameterization and specificationAbstraction is language independentAny programming language can be usedObject-oriented languages make things easierThe data abstraction is central for OO programming and design
16
Outline
Course organizationIntroductionJava overview
17
Java overview
Reference implementation by SUNPlatform independentCompiled into bytecode (kind of assembler)Programs run on a virtual machine JVMAutomatic memory management -garbage collection
18
OO abstractions in JavaSpecification => interfaces, javadoc, assertionsParameterization => methods, classes
Data abstractions => classes & objectsProcedural abstractions => methodsIteration abstractions => loops (for, while, do), iteratorsType hierarchies => class inheritance
19
Java programs
Consist of classes and/or an interfacesCollections of proceduresData abstractions
No global variables, no “free” functionsEach class in its own fileInterface and implementation in one file (no .h files)
20
Classes
Consist of fields and methodspublic class Square extends Shape {
private float xCoordinate;…public void rotate(float degree) {…
}…}
21
Java TypesClasses - system and user definedPrimitive types - system defined:
byte, short, int, long, (8,16,32,64 bit), boolean, char (16 bit), float, doublefor performanceCan be wrapped in classes
Arrays:Are objects<type>[] <name>;String names[];Can’t grow
22
Objects and variablesData accessed through variablesContain objects (on heap) or values (on stack)Primitive and reference variablesObject creation - the new operatorEquality: ‘==‘ and ‘equals’Object identityMutability and sharingArray objects
23
Class fieldsDescribe state of objects and classes<visibility> <type> <name> = <value>;public float xCoordinate = 74.3;Accessed through objectssomeObject.fieldNameStatic fileds - value shared by all class objectsstatic <visibility> <type> <name> = <value>;Accessed through class: someClass.fieldName
24
Methods
Method declaration and creation<visibility> <return type> <name>(formal params)
{ body; }
Call semanticsexpr.someMethod(param1, …);
Class vs instance methodsConstructor methods
Initialize objectsUsed through new operator
25
Packages
Group related classes and interfacesProvide for modularityClass/interface visibilityFully qualified names
26
Known syntax
for, while, doswitchNo gotoNo pointers
27
Java applications
Console applicationsGUI applicationsStandalone vs. applets
28
Java applications:the main method
Entry point to run applicationsStatic public main(String[] args){…}One per class
testingalternative program versions
29
Java applications:minimal Java programclass hello {
public static void main (String[] args) {
System.out.println(“Hello world!”);System.exit(0);
}}