An object is a collection of related variables and methods (functions).
Objects exist at runtime in the computer's memory.
We can refer to the members of an object by qualifying their names with the object's name:
myAccount.balance
myAccount.deposit(3.14);
The second line is an example of a method invocation. We can think of method invocation as a request for an object to execute one of its methods.
The members of an object can have private (class), package, protected (package + subclass), or public (program) scope.
A method consists of an operation (signature, prototype) and an implementation (body, block):
double square(double x) // operation
{ return x * x; } // implementation
An interface is a collection of related operations:
interface IAccount {
double getAmount();
void deposit(double amt);
void withdraw(double amt);
}
A class is a collection of related variable and method declarations. A class can implement an interface by providing implementations for the operations contained in the interface:
class Account implements IAccount {
private double balance = 0;
public double getAmount() {
return this.balance;
}
public void deposit(double amt) {
this.balance += amt;
}
public void withdraw(double amt) {
this.balance -= amt;
}
// etc.
}
Implicit argument:
this = reference to the object executing the method
Here's the UML notation for a class implementing an interface:
A class can be thought of as a template for creating objects:
Account myAccount = new Account();
Terminology: myAccount is an instance of or instantiates the Account class.
Recall that domain model associations represent relationships.
Design model associations represent collaborations.
An association between class A and class B:
means that instances of A provide services to instances of type B and vice versa.
A uni-directional association from A to B:
means that instances of B provide services to instances of A, but not vice versa.
More formally, we say that A is B's client or B is A's provider:
An association would typically translate into a member variable in the client class containing a reference or pointer to an instance of the provider class.
In Java:
class Client {
private Provider myProvider;
public void method1() {
myProvider.serviceA();
myProvider.serviceB();
// etc.
}
// etc.
}
In C++:
class Client
{
private:
Provider* myProvider;
public:
void method1()
{
myProvider->serviceA();
myProvider->serviceB();
// etc.
}
// etc.
};
Designers have to be careful about choosing between uni-directional and bi-directional associations. In general, uni-directional associations are easier to manage.
Recall that domain model generalizations imply that subclass instances can substitute for super-class instances.
The interpretation of design model generalizations is the same.
In the following example Calculator has three subclasses:
In Java generalization translates into the extends relationship:
class MathCalculator extends Calculator {
public void sin() {
result = Math.sin(result);
}
// etc.
}
In C++ generalization translates into the derived class relationship:
class MathCalculator: public Calculator
{
public:
void sin()
{
result = sin(result);
}
// etc.
};
Note that MathCalculator can access the result member variable inherited from Calculator because it is protected instead of private.
A variable of type Calculator can contain a reference to a MathCalculator or an EngineeringCalculator:
Calculator calc = new ScientificCalculator();
This is made possible by inheritance: a subclass inherits the members of its super-class. For example, assume we create a Math Calculator:
MathCalculator mathCalc = new MathCalculator();
We can call all of the methods inherited from Calculator:
mathCalc.add(3.14);
mathCalc.mul(-2.1);
Although the calc variable contains a reference to a scientific calculator, we can't call any scientific calculator methods:
calc.force(); // ERROR: force not a member of Calculaor class
In this case we would need to temporarily retype calc as a scientific calculator:
((ScientificCalculator)calc).force(); // ok
A package is a collection of related classes and subpackages. For example, Java's Ellipse2D class is contained in the geom subpackage of the awt subpackage of the java package. Here's the qualified name for Ellipse2D:
java.awt.geom.Ellipse2D
Here's the UML icon for a package:
The dashed arrow indicates that some of the members of the gui package use some of the members in the geom package. We say that gui imports from geom or that geom exports to gui.