Top Banner
UML Basics & Access Modifier UML Basics
30
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

UML Basics & Access Modifier

UML Basics

Page 2: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Java: UML Diagrams

EVE does Java

Page 3: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

UML Diagram – Class & Object

A class is described using a rectangle box with three sections.

The top section gives the class name, the middle section describes the fields, and the bottom section describes the methods. The middle and bottom sections are optional, but the top section is required.

An object is described using a rectangle box with two sections.

The top section is required. It gives the object’s name and its defining class. The second section is optional; it indicates the object’s field values.

Page 4: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

The Access Modifiers The symbols +, - and # are used to denote,

respectively, public, private, and protected modifiers in the UML. The static fields and methods are underlined.

Page 5: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

The Access Modifiers -- public

public access modifier: the + sign in UML Fields, methods and constructors declared public

(least restrictive) within a public class are accessible by all class in the Java program, whether these classes are in the same package or in another package.

For simplicity, just take package as the file folder in Windows

Note: in one application program, there is only and only one public class

Fields/methods are called Class fields/method/member, i.e., you don’t need to create an instance to use them; the convention is to use the classname.methodName notation

Page 6: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

The Access Modifiers -- private

private access modifier: the – sign in UML The private (most restrictive) fields or

methods cannot be accessed outside the enclosing/declaring class. To make all fields private, provide public getter/setter methods for them.

The getter is used to set the values of the data fields

The setter is used to change/modifier the fields

Page 7: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

The Access Modifiers -- protected

protected access modifier: the # sign in UML

Fields, methods and constructors declared protected in a class can be accessed only by subclasses in other packages and by those in the same class even if they are not a subclass of the protected member’s class.

Page 8: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

The Access Modifiers -- default

default access modifier: without any sign

when no access modifier is present, any class, field, method or constructor are accessible only by classes in the same package.

This is also called the instance members, i.e., must create an instance of the class before the fields/methods can be used

Use the instanceName.method() format

Page 9: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

What is the Modifier? +constructor()

Public Private Final Protected Static Default

Page 10: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Good!What is this Modifier? -radius: double

Public Private Final Protected Static Default

Page 11: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Good!What is this one? + numberOfObjects(): int

Public

Private

Final

Protected

Static

Default

Page 12: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Good!What is this one? # aMethod(): void

Public

Private

Final

Protected

Static

Default

Page 13: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Answer the following: How do you tell which method is a

constructor? To access private methods, you need public _______ and ________ methods. (Also called Accessors and Mutators)

A modifier with no symbol is accessible only within the same folder or __________.

see

see

see

Page 14: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Now let’s build a program The name of the

class will be, of course, Rectangle.

class Rectangle

Page 15: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Rectangle class

class Rectangle

{

// Data members

private double width = 1, height = 1;

private static String color = "yellow";

It has two private or local variables, and a static or global variable that is shared.

Page 16: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Rectangle class

// Constructor public Rectangle()

{

}

The default constructor (with no parameters) - it will use the initialized values for height, width, and color.

Page 17: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Rectangle class

// Constructor

public Rectangle(double width, double height)

{

this.width = width

this.height = height;

}

The Rectangle constructor needs two doubles.

If you wish to use the same name in the parameters (formal and actual) you use this.

Page 18: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Rectangle class

public double getWidth()

{

return width;

}

In order to access or make changes to private variables, you need sets and gets.

(Accessors and Mutators)

Page 19: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Rectangle class

public double getWidth()

{

return width;

}

public double getHeight()

{

return height;

}

This is to access a Rectangle object’s height and width after it is constructed.

Page 20: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Rectangle class

public static String getColor()

{ return color;

}public static void setColor(String color)

{ Rectangle.color = color;

}

Color is static- it can be shared – and changed- with all rectangle objects.

Page 21: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Rectangle class

public double getArea() { return width * height;}public double getPerimeter() { return 2 * (width + height);}

Now we just need the methods to get the area and perimeter of a rectangle object.

Page 22: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

Rectangle class

class Rectangle { // Data members private double width = 1, height = 1; private static String color = "yellow";

// Constructor public Rectangle() { }

// Constructor public Rectangle(double width, double height) { this.width = width; this.height = height; }

public double getWidth() { return width; }

public double getHeight() { return height; }

public static String getColor() { return color; }

public static void setColor(String color) { Rectangle.color = color; }

public double getArea() { return width * height; }

public double getPerimeter() { return 2 * (width + height); }}

Now put it all together and compile it.

Did you get it?

Page 23: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

TestRectangle class

public class TestRectangle

{ public static void main(String[] args)

{

To see if it works you now have to create a TestRectangle class.

- which includes main.

Page 24: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

TestRectangle class

Rectangle myRect1 = new Rectangle();

Rectangle myRect2 = new Rectangle(4, 40);

Rectangle myRect3 = new Rectangle(3.5, 35.9)

Let’s create three rectangles, one default and two with different parameters.

Page 25: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

TestRectangle class

System.out.println("The area of the rectangle is "

+ myRect1.getArea());

System.out.println("The perimeter is "

+ myRect1.getPerimeter());

System.out.println("The color is "

+ myRect1.getColor());

To see if it worked, let’s print out the first one. Compile and run. Get it?

Page 26: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

TestRectangle class

The area of a rectangle with width 1.0 and height 1.0 is 1.0

The perimeter of a rectangle is 4.0

The color is yellow

This is the rectangle object with no parameters. You can print out the height and width if you wish.

Page 27: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

TestRectangle class

System.out.println("The area of the rectangle is "

+ myRect2.getArea());

System.out.println("The perimeter is "

+ myRect2.getPerimeter());

System.out.println("The color is "

+ myRect2.getColor());

Now try the next rectangle using (4,40).

Page 28: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

TestRectangle class

The area of the rectangle is 160.0ÏϧÏThe perimeter is 88.0ÏϧÏThe color is yellow

Did you get it?

Page 29: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

TestRectangle class

myRect3.setColor("red"); System.out.println("The area of the rectangle is " + myRect3.getArea());

System.out.println("The perimeter is " + myRect3.getPerimeter());

System.out.println("The color is " + myRect3.getColor());

Let’s try something a little different. Set the color for the third rectangle to

“red” before you print it out.

Page 30: UML Basics & Access Modifier UML Basics. Java: UML Diagrams EVE does Java.

TestRectangle class

ÏThe area of the rectangle is 125.64999999999999ÏϧÏThe perimeter is 78.8ÏϧÏThe color is redÏϧÏ

Did you get it?.