Title: COMPE 438 JAVA PROGRAMMING
1COMPE 438JAVA PROGRAMMING
2INTRODUCTION TO OBJECT-ORIENTED PROGRAMMINGTHE
UNIFIED MODELING LANGUAGE, UML
3Desirable Qualities of Software Systems
- Usefulness
- Timeliness
- Reliability
- Maintainability
- Reusability
- User friendless
- Efficiency
4Desirable Qualities of Software Systems with OO
focus
- Usefulness
- Timeliness
- Reliability
- Maintainability
- Reusability
- User friendless
- Efficiency
- Cost
- Reliability
- Flexibility
5Change or die
- Software
- is not like a novel that is written once and then
remains unchanged. - is extended, corrected, maintained, ported,
adapted - cannot be maintained will be thrown away.
- The work is done by different people over time
- Either it is continuously maintained
- or it dies.
6Object-Oriented Programming
- a programming methodology that views a program as
consisting of objects that interact with one
another by means of actions - alternative to procedural programming
- Address problems of procedural programming and
top-down design - Cascading changes
- Decomposition based on the structure of objects,
classes and the relationships
7Object-Oriented Programming
- Everything is an object
- A program is a bunch of objects telling each
other what to do by sending messages - Each object has its on memory made up of other
objects - Every object has a type
- All objects of a particular type can receive the
same message
8Objects
- Reusable software components that model
real-world items - Look all around you
- People, animals, plants, cars, etc.
- Attributes
- Size, shape, color, weight, etc.
- Behaviors
- Babies cry, crawl, sleep, etc.
9Object-Oriented Analysis and Design (OOA/D)
- Essential for large programs
- Analyze program requirements, then develop
solutio - Unified Modeling Language (UML
- Graphical language that uses common notation
- Allows developers to represent object-oriented
designs - Flexible and extendible
10History of UML
- Need developed for process with which to approach
OOA/ - Brainchild of Booch, Rumbaugh and Jacobson
- Object Management Group (OMG) supervised
- Version 1.6 is current version
11Classes and Objects
In Real World
In OO Model
represents anything in the real world
that distincly identified
has an indetity, a state and a behavior
OBJECT
represents a set of objects with similar
characteristics and behavior. These objects Are
the instances of the class
characterizes the structure of states and
behaviors that are shared by all its instances
CLASS
12Classes, Objects, Instances
- Class
- Describes the structure and behavior of a set of
similar objects - Object
- an instance which is present at execution time
and allocates memory for its instance variables,
and which behaves according to the protocol of
its class
Class
Object
13Classes, Objects, Instances
- Object-Class relationship
Class
Object
instance of
Shape
Triangle
instance of
14Classes, Objects, Instances
- Properties of classes and objects
- Attributes
- The structure of the objects
- Circle -gt Radius and position (x,y)
- Operations
- The behavior of the objects
- Circle -gt display, remove, reposition, resize
- Constraints
- Conditions, requirements and rules that objects
must satisfy - Circle -gt radius gt0
15Classes, Objects, Instances
Circle
Class name
Constraint
radius radius gt 0 centerpoint Point (10,
10)
Attribute name
Initial Value
display() remove() setPosition(pos
Point) setRadius(newRadius)
Attribute type
Parameters (name type initial value)
Methods
Class
16Classes, Objects, Instances
class Circle int radius Point
centerPoint public void setRadius(int
newRadius) if(newRadius gt 0)
//constraint radius
newRadius public
void setPosition()
public void display()
public void remove()
17Classes (example)
Point
int x
UML
int y
public void move(int dx, int dy)
Java Code
18Classes, Objects, Instances
aCircle Circle
Object name
Class name
radius 25 centerpoint (10, 10)
Attribute names
Attribute values
Object
19Examples for Objects
p1Point
p2Point
UML
x 0
x 24
y 0
y 40
Java Code
20Packages
- classes are grouped into a package
- organized into a hierarchy
- closely related classes
21UML Notation for Packages
java.awt
datatransfer
Point
event
image
22Class Hierarchies
23Association
Company
Person
0..1
0..
employs
24Aggregation
Enterprise
Department
Employee
consists of
consists of
1
1..
1
1..
25Composition Aggregation
Invoice
Invoice Item
has
composition
1
1..
Car
Wheel
has
aggregation
1
3..4
26The Principles in OOP
- Modularity
- Abstraction
- Encapsulation
- Polymorphism
27The Principles in OOP
- Modularity
- High Cohesion
- Low Coupling
- Abstraction
- Encapsulation
- Polymorphism
28The Principles in OOP
- Modularity
- Abstraction
- Contractual interface
- Ex telephone service with manual as abstraction
- Encapsulation
- Polymorphism
29The Principles in OOP
- Modularity
- Abstraction
- Encapsulation
- Polymorphism
30Encapsulation
- separate implementation from its contractual
interface - reduce coupling
- e.g. Customer, waiter and kitchen
31The Principles in OOP
- Modularity
- Abstraction
- Encapsulation
- Polymorphism
32Polymorphism
- one of the cornerstones that makes
object-orientation powerful - having many forms
- an operation may behave differently (in different
classes)
33Polymorphism
Shape
...
draw()
Circle
Rectangle
...
...
draw()
draw()
34Polymorphism
- Objects can respond differently to the same
message. Both waiter as kitchen respond to 'a
black coffee. - The actions are different though.
35Inheritance
- one of the most important relationship in OO
modeling - parent/child relationships among classes
- supports a form of code reuse
- precondition for polmorphism
- types of inheritance
- single inheritance
- multiple inheritance
- Java does not support multiple inheritance
36Inheritance
37Inheritance
- Both waiter and cook are employee. So they both
have an employee number. - Both return a cup of coffee to the question "A
cup of Coffee please". - There are some exceptions. Waiter and Cook have
different methods to get a cup of coffee.
38Inheritance
class Person private String name public
void setName(String n) name
n public String getName() return
name
Person
Student
class Student extends Person private String
stuNum public void setStuNum(String
sn) stuNum sn public String
getStuNum() return stuNum
39Inheritance
public class TestInheritance public static
void main(String args) Student stu new
Student() stu.setName("John
Smith") stu.setStuNum("12345") Syste
m.out.println("Student Name" stu.getName())
System.out.println("Student Number
stu.getStuNum())
40Multiple Inheritance
Animal
Frog
Dinosaur
Frogosaur
41Multiple Inheritance
- abstract class Animal abstract void talk()
-
- class Frog extends Animal void talk()
System.out.println("Ribit, ribit.") -
-
- class Dinosaur extends Animal void talk()
- System.out.println("Oh I'm a dinosaur and
I'm - OK...")
-
-
- // (This won't compile, of course, because Java
// only supports single inheritance.) - class Frogosaur extends Frog, Dinosaur
Animal animal new Frogosaur() animal.talk()
?