Object Oriented Analysis and Design Using the UMLtoJava

Object Oriented Analysis and Design Using the UML-to-Java Mapping OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 1

Java Features w The slides in this presentation include some suggestions for UML to Java mappings. w Most of this class knows Java – but not all. w So, I will try to elaborate upon the Java language features as they are encountered in support of the slides containing UML that follow. w If you feel good about your personal knowledge of this material, feel free to leave and use your time, perhaps, more profitably. OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 2

Mapping Representation: Notes // // Notes will be used in the rest of the presentation to contain Java code for the attached UML elements public class Course { Course() { } protected void finalize() throws Throwable { All Java programs are defined using class definitions. super. finalize(); } All Java apps have a main method (not shown here) This }; is an example of simply a java class. Course() is the Constructor that is executed when objects of this An exception may be thrown in Course in finalize(). A throw statement is used to begin exception propagation class are instantiated. Constructors do not return a value and is well beyond where we are here. and do not have a return type… See pp. 457 – on current Java text used at UNF. OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 3

Visibility for Attributes and Operations • Note what the UML translates into. Student • Notice the visibility and how - name : String this is translated into private protected, public. . + add. Schedule (the. Schedule: Schedule, for. Semester: Semester) + has. Prerequisites(for. Course. Offering: Course. Offering) : boolean # passed(the. Course. Offering: Course. Offering) : boolean • Public boolean returns true or false. • Method bodies not shown. public class Student { private String name; public void add. Schedule (Schedule the. Schedule; Semester for. Semester) { } public boolean has. Prerequisites(Course. Offering for. Course. Offering) { } protected boolean passed(Course. Offering the. Course. Offering) { } } OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 4

Class Scope Attributes and Operations Student - next. Avail. ID : int = 1 class Student { private static int next. Avail. ID = 1; + get. Next. Avai. IID() : int } public static int get. Next. Avai. IID() { } The location at which a variable is declared defines its scope, which is the area within a program in which that variable can be referenced. By being declared at the class level (not w/in any method), these variables and constants can be referenced in any method of the class (or outside the class, if not private). Attributes declared within methods are called instance data because memory space is created for each instance of the class that is created. Above, we have a class variable (private) and a public class method, get. Next. Avail. ID() created from the UML. ‘static’ in Java means ‘at the class level. ’ In Java, the static modifier associates a variable or method with its class rather than with an object of the class. OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 5

Utility Class w A grouping of global attributes and operations – recall, we don’t instantiate these – hence ‘static. ’ w Here, we are invoking several class methods (static methods) w Math class is part of the Java standard class library and is defined in the java. lang package. Below, we have a utility class called Math. Pack, which appears to be importing java. lang. Math and java. util. Random w Reserved word static implies that the method can be invoked through the name of the class, as in: Math. cos(angle) (below) import java. lang. Math; import java. util. Random; class Math. Pack { private static random. Seed long = 0; private final static double pi = 3. 14159265358979; public static double sin(double angle) { return Math. sin(angle); } static double cos(double angle) { return Math. cos(angle); } static double random() { return new Random(seed). next. Double(); <<utility>> Math. Pack -random. Seed : long = 0 -pi : double = 3. 14159265358979 +sin (angle : double) : double +cos (angle : double) : double +random() : double void somefunction() {. . . my. Cos = Math. Pack. cos(90. 0); . . . OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 6 } }

Nested Class w Hide a class that is relevant only for implementation class Outer { public outer() { } Outer: : Inner class Inner { public Inner() { } } • Can declare a class inside another class (just like a loop). Nested classes are considered a member of the enclosing class. • Creates a separate bytecode file; has the extension. class and is referenced via: Enclosing$nested. class. • Nested class has access to enclosing class’s instance variables and methods, even if declared with private visibility. But enclosing class can access data in nested class only if data is declared public. Nested classes is the exception to declaring data ‘public. ’ It is normal to declare data of a private nested class ‘public’ because only the enclosing class can get to that data (despite its public declaration. ) OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 7

Associations w Bi-directional associations; Two classes in package. w Note the declarations in each class needed to support bidirectional associations…both have public methods (to ‘know’ each other) and private data. w Create objects of other class. // no need to import if in same package Schedule class Schedule { public Schedule() { } //constructor private Student the. Student; } class Student { public Student() { } private Schedule the. Schedule; Student } OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 8

Association Navigability w Uni-directional associations § Student knows about schedule; Student is a client of Schedule. § Can Create objects of other class. Schedule class Schedule { public Schedule() { } } class Student { public Student() { } private Schedule the. Schedule; } Student OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 9

Association Roles Professor instructor Course. Offering class Professor { public Professor() {} private Course. Offering the. Course. Offering; } class Course. Offering { public Course. Offering() {} private Professor instructor; } • Adding a role indicator: Each class ‘knows’ about the other class. • For class Course. Offering, this contains a public method, Course. Offering() and a private declaration of an object (instructor) of type Professor. • For class Professor, this contains a public method, Professor, and the creation of a private object the. Course. Offering of type Course. Offering. OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 10

Association Multiplicity Course. Offering 0. . 4 class Course. Offering { public Course. Offering() {} } primary. Courses Schedule class Schedule { public Schedule() {} private Course. Offering[] primary. Courses = new Course. Offering[4] } • Here we are showing how multiplicity in UML is accommodated in Java. • Have class Course. Offering containing a public method, Course. Offering(). • In class Schedule, in addition to the public method, Schedule(), we have a new array of four Course. Offering objects each object is of type Course. Offering. OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 11

Association Class // No need to import if in the same package alternate. Courses 0. . * class Primary. Schedule. Offering. Info { public Primary. Schedule. Offering. Info() {} 0. . 2 Schedule primary. Courses public Course. Offering get_the. Course. Offering(){ return the. Course. Offering; } Course. Offering 0. . 4 0. . * public void set_the. Course. Offering(Course. Offering to. Value){ the. Course. Offering = to. Value; } Primary. Schedule. Offering. Info - grade: char = I } private char get_Grade (){ return grade; } private void set_Grade(char to. Value) { grade = to. Value; } private char grade = ‘I’; private Course. Offering the. Course. Offering; • Remember, an association class is a class that is connected to an association. There is an instance of the association class for every instance of the relationship (e. g. , for every link). Design Decisions 0. . * Schedule alternate. Courses 0. . 2 primary. Course. Offering. Info 1 0. . 4 Primary. Schedule. Offering. Info - grade: char = I OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 12 0. . * 1 Course. Offering

Association Class // No need to import if in the same package alternate. Courses 0. . * class Primary. Schedule. Offering. Info { public Primary. Schedule. Offering. Info() {} 0. . 2 Schedule primary. Courses Course. Offering 0. . 4 0. . * Primary. Schedule. Offering. Info - grade: char = I } public Course. Offering get_the. Course. Offering(){ return the. Course. Offering; } public void set_the. Course. Offering(Course. Offering to. Value){ the. Course. Offering = to. Value; } private char get_Grade (){ return grade; } private void set_Grade(char to. Value) { grade = to. Value; } private char grade = ‘I’; private Course. Offering the. Course. Offering; • During design, some decisions are made regarding navigation between the involved classes. A subset of the class operations and attributes are shown above. For this example, we included a subset to demonstrate the UML construct we are emphasizing. Design Decisions 0. . * Schedule alternate. Courses 0. . 2 primary. Course. Offering. Info 1 0. . 4 Primary. Schedule. Offering. Info - grade: char = I OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 13 0. . * 1 Course. Offering

• • Reflexive Associations prerequisites 0. . * Course import java. util. Vector; class Course { public Course() {} // The unbounded multiple association // is stored in a vector private Vector prerequisites; } A class may have an association with objects of the same type, as we know. Here is the corresponding Java realization of that realization. Vector is a class in java. util. Vector is a public class … that manages an array of objects. Elements can be added or removed from this list and the size of the list can change dynamically. • Objects of type Vector include methods such as copy. Into, element. At, contains, insert. Element. At, add. Element and much more. OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 14

Aggregation class Schedule { public Schedule() { } private Student the. Student; } Schedule 0. . * import java. util. Vector; 1 Student class Student { public Student() { } private Vector the. Schedule; } • Vector is a reusable list class available in the Java programming environment. • ‘Student’ class declares an reusable list class of type ‘Vector. ’ • (Java has no explicit construct for aggregation. So it treats aggregation kind of like an array of objects) • In Java, the code for aggregation looks the same as it does for “vanilla” association, where each class ‘knows about’ each other. . . ) OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 15

Composition class Schedule { public Schedule() { } private Student the. Student; } Schedule 0. . * import java. util. Vector; 1 Student class Student { public Student() { } private Vector the. Schedule = new Vector(); } Java does not support containment by value. the. Schedule is a new class list created by Student… OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 16 This part is the same as aggregation…

Interfaces and Realizes Relationships <<Interface>> Serializable interface Serializable { } <<entity>> Schedule class Schedule implements Serializable { } • Java has the notion of ‘implements’ which translates to the ‘realizes relationship’ in the UML. • Java classes implement interfaces and they can implement as many interfaces as they need to. • Interfaces can extend other interfaces. • In Java, interfaces may have attributes defined for them. This differs from pure UML definition of interface which states, “An interface is a declaration of a collection of operations that may be used for defining a service offered by an instance. Interfaces may not have attributes, associations, or methods. ” OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 17

Generalization class Ground. Vehicle { public int license. Number; public void register() { } } Ground. Vehicle +license. Number: int +register() class Truck extends Ground. Vehicle { public float tonnage; public void get. Tax() { } } Truck +tonnage: float +get. Tax() • Remember: generalization is a ‘is-a’ or ‘kind of’ type of association. • It is used to represent a generalization type of association • Generalization is modeled as an open triangular arrowhead pointing to the base of the “base class” end. • Java has the notion of ‘extends’ which translates to the generalization relationship in the UML. • Java classes can extend ONE other class. • Interfaces can extend other interfaces. Java interfaces are discussed ahead…. OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 18

Multiple Inheritance In Java, a class can only inherit from one superclass. It can, however implement multiple interfaces <<Interface>> IVehicle {overlapping} <<realize>> Land Vehicle <<Interface>> IWater. Vehicle <<realize>> interface IWater. Vehicle : extends IVehicle {. . . } class Amphibious. Vehicle extends Land. Vehicle implements Water. Vehicle {. . . } Amphibious Vehicle • In Java, a class can only inherit from ONE superclass. However, a class can realize multiple interfaces. • Remember, subclasses not mutually exclusive can be annotated with the UML {overlapping} constraint. • This supports multiple inheritance. OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 19

Multiple Inheritance (contd) <<Interface>> ILand. Vehicle <<Interface>> IWater. Vehicle Land. Vehicle <<Interface>> IAmphibious. Vehicle Java supports multiple inheritance between interfaces. In Java, an interface CAN inherit from multiple interfaces. Multiple inheritance of interfaces overcome the ‘weakness’ of Java An interface can inherit regarding true multiple from many interfaces. inheritance. In Java, a class can inherit from one superclass. It can, however implement multiple interfaces. Amphibious. Vehicle OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved <<Interface>> IHobby 20

Abstract Class abstract class Animal { public abstract void talk(); } Animal {abstract} +talk() {abstract} Lion Tiger +talk() class Tiger extends Animal { public Tiger() { } public void talk() { } } Remember, an abstract class is a class for which no instances are created. OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 21

Parameterized Class Java does not support parameterized classes T, n: int Set insert(T) remove(T) <<bind>> <float, 100> my. Set. Of. Floats OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 22

Subsystems <<subsystem>> Course. Catalog ICourse. Catalog get. Course. Offerings() : Course. Offering. List package Course. Catalog; There can be only one public class per file. (You can have inner classes in the file as well, but only one ‘top level’ class). The name of the file must be the same as the name of the public class. public interface ICourse. Catalog { public Course. Offering. List get. Course. Offerings(); } OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 23

Subsystems <<subsystem>> Course. Catalog ICourse. Catalog get. Course. Offerings() : Course. Offering. List package Course. Catalog; public interface ICourse. Catalog { public Course. Offering. List get. Course. Offerings(); Note: Course. Offering. List is assumed to exist in a separate common package. The import statement has been excluded from the code fragment. } OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved These restrictions are what hinder Java’s support for subsystems. In the UML, the mapping between interfaces and subystems is many to many (subsystems can realize one or more interfaces; interfaces can be realized by one or more subsystems). In Java the mapping is always one-to-one. 24

Subsystems and UML <<subsystem>> Course. Catalog ICourse. Catalog get. Course. Offerings() : Course. Offering. List As we have discussed w/i OOAD course subsystems are the Design Model representation for components. package Course. Catalog; Some aspects of UML subsystems can be implemented using Java packages have a 1 -1 correspondence to UML packages. public interface ICourse. Catalog { public Course. Offering. List get. Course. Offerings(); } OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 25

Packages… w Packages in Java also correspond to directories. w Packages are declared at the top of the file. w All classes defined within the file are considered part of the specified package. w All classes defined within the same package can see each other automatically. w If the package statement is omitted (i. e. , no package is specified), the file contents are considered to be in the ‘default package’ (e. g. , the root package), and all other classes for which a package was not specified can see the classes defined within a file. OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4. 2 Copyright Ó 1998 -1999 Rational Software, all rights reserved 26
- Slides: 26