mario::konrad
programming / C++ / sailing / nerd stuff
UML and C++ / Java
© 2004 / Mario Konrad

Introduction

The purpose of this summary should give you an idea about the use of UML and how to implement it with C++ and Java. Sometimes there are more than one way to implement a thing, one example will be provided.

This document is not intended to be a programming or UML tutorial. It is more to show you the bridge between UML and C++, Java respectively.

Since this document deals with object oriented paradigma, procedural aproaches, like the programming language C, are not covered.

Class

UML

C++

class Foo
{
    private:
        int count;
    protected:
        int size;
    public:
        Foo();
        int getCount() const;
};

Java

public class Foo
{
    private int count;
    protected int size;
    public Foo() { ... }
    public int getCount() { ... }
}

Generalization / Specialization

UML

C++

class Foo { ... };
class Bar : public Foo { ... };

Java

public class Foo { ... }
public class Bar extends Foo { ... }

Interface

UML

C++

Not supported, besides you use a pure virtual base class (no attributes, no private or protected methods, no implementations of methods).

class Foo
{
    public:
        virtual void doSomething(void) = 0;
        virtual int doSomethingOther(void) = 0;
};

class Bar : public Foo
{
    public:
        virtual void doSomething(void);
        virtual int doSomethingOther(void);
};

void Bar::doSomething(void) { ... }:
int Bar::doSomethingOther(void) { ... };

Java

public interface Foo
{
    public void doSomething();
    public int doSomethingOther();
}

public class Bar implements Foo
{
    public void doSomething() { ... }
    public int doSomethingOther() { ... }
}

Object

UML

C++

Foo foo;
Foo * foo = new Foo;

Java

Foo foo = new Foo;

Aggregation

UML

C++

class Foo { ... };
class Bar
{
    private:
        Foo * foo;
};

Java

public class Foo { ... }
public class Bar
{
    private Foo foo;
}

Composition

UML

C++

class Foo { ... };
class Bar
{
    private:
        Foo foo;
};

Java

Not supported in Java. The lifetime of a composite object depends completely on the garbage collector.

Dependency

UML

C++

class Foo { ... };
class Bar
{
    public:
        void doSomething(void);
};

void Bar::doSomething(void)
{
    Foo foo;
    Foo * foo1 = new Foo;
    // ...
    delete foo1;
}

Java

public class Foo { ... }
public class Bar
{
    public void doSomething()
    {
        Foo foo = new Foo();
    }
};

Multiplicity and Direction

UML

C++

class Foo { ... };
class Bar
{
    private:
        Foo (*foo)[3];
        std::vector<Foo *> foos;
};

Java

public class Foo { ... }
public class Bar
{
    private Foo [] foo = new Foo[3];
    private Vector foos = new Vector(); // contains Foo objects
}

Multiple Inheritance

UML

C++

class Foo { ... };
class Bar { ... };
class FooBar : public Foo, public Bar { ... };

Java

Not supported.

Association Class

UML

C++

class Foo { ... };
class Bar
{
    private:
        std::vector<Foo *> foos;
};

Java

public class Foo { ... }
public class Bar
{
    private Vector foos = new Vector();  // contains Foo objects
}

Parametrized Class

UML

C++

template< class T > class Foo { ... };
template< class T > class Bar
{
    private:
        T data;
};
class SpecialBar : public Bar< int > { ... };
template< class T, int size > class Array
{
    private:
        T data[size];
};

Java

Supported by Java 1.5 and newer.

class Foo< T >{ ... };
class Bar< T >
{
    private T data;
}
class SpecialBar extends Bar< Integer > { ... }
class Array< T, Integer >
{
    private T data[];
}