Java Fundamental period - Lecture 2

Object-Oriented Programming and Java fundamentals

Review of the last lecture

What we need as programmers : general needs

Basically, the utmost need is to be able to communicate in a professional environment

What we need as programmers : diagramming needs

In OOP (and almost in any kind of programming), we have to cover three types of diagram:

Solution matching our needs: UML

UML allows to give an answer to each of these needs

UML: Class diagrams

The class diagram allows to show the class organization, how they depend between each other, what are their interactions, etc.
This brings these questions:

Class definition : properties

The first description of a real "object" is a factual description, with an enumeration of each property of that object
This property set is also called fields of that object
The dog object example

Class interactions : Messages and methods

The most basic interaction with an object is to send a message (an instruction) to it in order for this object to perform an operation.
To be able to receive and to treat this message, this object has to have a method corresponding to this message

Class interactions : Exercise

Remember our contact phone book? use the class diagram to represent each class, without representing their interaction

Class interactions : relations between classes

In UML each class relation is represented by a specific "link"
What are the different relations?

Class interactions : Association

An association between two classes is symbolized by a link between those two classes

Class interactions : Association(2)

An association has a cardinality, indicating how many object references the association is concerning
This should be read as: "An account can have up to two owners, and a person can have several accounts"

Class interactions : Aggregation

An aggregation is the fact for a class to be composed with an other class
There are 2 types of aggregation

Class interactions : the Weak Aggregation

A good example of a weak aggregation is the association between a Playlist and the Tracks of that playlist
Exercise : define the cardinality of the Playlist - Track association

Class interactions : the Weak Aggregation (2)

The weak aggregation is when:

The Weak Aggregation (3)

Class interactions : the Composition link

The composition is a sort of aggregation where the composite and the component objects are strongly linked

Class interactions : the Composition link

The composition is a sort of aggregation where the composite and the component objects are strongly linked
The composition link implies that :
In general, the aggregation link is known as the HAS-A relationship

Class Hierarchy

Class Hierarchy (2)

The inheritance mechanism is very useful to factor the common properties and methods shared by several objects
The inheritance is also known as the "IS-A" relationship
The previous sentence can be represented as follows

Class Hierarchy (3)
Specialization vs Generalization

You can read the diagram from right to left, in this way the description is called Generalization
In the other way, you start from a general Object and you finish on a specific one, this is called Specialization

When you specialize an object, the result object owns all the characteristics of its super-class plus its own characteristics

Class Hierarchy : Exercise

What are the properties and methods belonging to the wolf, according to the schema ?
What if I should include the Dog in that Class Diagram?

Class Diagram: (Real) Exercise

Solution

UML - Activity Diagrams

Activity diagrams are used to define a succession of operations, describing the algorithm of the application
We will complete that section after a few time on eclipse