Object-Oriented Basics and UML

Read the "Java Access Specifiers" (starting on p. 255) in Thinking in Java
Read the sections "What is an Object" and "What is a class" here:
http://java.sun.com/docs/books/tutorial/java/concepts/index.html

UML
(Unified Modelling Language) - Notation for basic constructs like classes and objects, and class relationships. We will use a simplified version of UML.




Basic Class Concepts



* What is an object?
A real-world object is an entity that can be distinctly and precisely identified and described.
An object in a software system is an abstraction of a real-world object. It consists of:
                            state + behavior + identity


state = data
behavior = actions the object performs
identity = some notion of identity - some means of distinguishing this object from another (e.g. a handle for the object)


Ex:
A Circle object
circle: x = 2, y = 3, radius = 5, area()



* What is a class?
A class is a set of objects with similar states and behaviors.
A class is a blueprint for these objects - it describes the structure of states and behaviors shared by all instances.
The class serves as a type.


Ex: A circle is a class of objects which have x, y, radius and such that the area can be calculated by pi*(radius)^2.


Ex: An order processing system
The Order class defines features that all orders have.
An Order object describes a specific order, e.g. the order placed by Harriet Johnson at 2:15 last Monday.


A class defines:
1. A public interface - a precise description of the services (or behaviors) provided by the class.
2. An underlying implementation for those services.

i.e. it defines a type (the public interface) and an implementation of that type (which is hidden)



Attributes and Behaviors



The state of an object includes its attributes. An attribute is a property such as color, shape, age, location, etc.


Ex: Attributes of a bicycle frame: color, pipe thickness, height
Each attribute has a type and a set of possible values.


A behavior is an action executed by an object - something you can ask an instance to do.

Behavior examples: people answer the phone, dogs bark, wheels roll

Behaviors are implemented in classes as methods.



UML Notation for Classes

Rectangle with up to 3 compartments

class name
field1
field2                (data members)
...
fieldn

method1
method2             (methods of class)
...
methodm



Ex:
class Point
{
   int x, y;
   public void move(int dx, int dy) {...}
}

Point
int x
int y

public void move(int dx, int dy)

Either or both of the last 2 compartments can be omitted.

Field descriptions look like this, where [] indicate optional values:
[Visibility] [Type] Identifier [= InitialValue]

Methods are described as follows:
[Visibility] [Type] Identifier ([parameter list])

Visibility can be: public, protected, private, or package

Other possible UML representations of Point class?



Exercise: Give the UML notation for these classes:
public class Animal
{
    int heartRate;
    float weight;
 
     public void move() {...}
     public void eat(String foodType) {...}
}

public class Dog extends Animal
{
    private String breed;

    public void bark() {...}
}

Note: An arrow indicates the subclass/superclass relationship.



UML Notation for an Object


objectName :className
attribute1
attribute2
...
attributen


Attributes specified by:
[Type] indentifier = value

In the UML:
:className can be omitted.
objectName can be omitted (:className denotes any object of class className)
Bottom compartment can be omitted.

Ex: Animal simon = new Animal();
simon.heartRate = 55;
simon.weight = 12.5;

UML for simon?




Message Passing

-Objects communicating with each other

Message = command sent to an object (called a recipient or receiver) to perform a certain action by invoking method of recipient

Message consists of:
Ex: simon.eat("rice")
Recipient: simon
method: eat()
arguments: "rice"



Modularity and Modules


module - a logical element of a system


A system should be decomposed into a collection of highly cohesive but loosely coupled modules.


Goal: Reduce coupling, increase cohesion.

In Java, modules are represented as classes, packages, and files.

Note: A module is a hierarchical concept. Modules are composed of other modules.