UML Basics & Access Modifier

Slides:



Advertisements
Similar presentations
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
Access to Names Namespaces, Scopes, Access privileges.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Writing Classes (Chapter 4)
A First Look At Classes Chapter 6. Procedural Programming In procedural- programming all the program functionality is written in a few modules of code.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Java Classes Using Java Classes Introduction to UML.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Page 4.1 – Autumn 2010Steffen Vissing Andersen SDJ I1, Autumn 2010 Agenda – Session 4 – 7. September 2009 Java fundamentals, checkpoint from chapter 2.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Topics Instance variables, set and get methods Encapsulation
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Class Fundamentals BCIS 3680 Enterprise Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
1 More About Derived Classes and Inheritance Chapter 9.
Chapter 15 Abstract Classes and Interfaces
Inheritance ITI1121 Nour El Kadri.
Introduction to Classes and Objects
Objects as a programming concept
Lecture 6 D&D Chapter 7 & 8 Intro to Classes and Objects Date.
CS Week 13 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Classes & Objects: Examples
Implementing Non-Static Features
Agenda About Homework for BPJ lesson 36 About practice of last class
Namespaces, Scopes, Access privileges
Introduction to Objects & Classes
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
CS 200 Creating Classes Jim Williams, PhD.
CS 200 More Classes Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CSG2H3 Object Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

UML Basics & Access Modifier

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.

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.

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

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

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.

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

What is the Modifier? +constructor() Public Private Final Protected Static Default

Good! What is this Modifier? -radius: double Public Private Final Protected Static Default

Good! What is this one? + numberOfObjects(): int Public Private Final Protected Static Default

Good! What is this one? # aMethod(): void Public Private Final Protected Static Default

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

Now let’s build a program The name of the class will be, of course, Rectangle. class Rectangle

Rectangle class It has two private or local variables, and a static or global variable that is shared. class Rectangle { // Data members private double width = 1, height = 1; private static String color = "yellow";

Rectangle class The default constructor (with no parameters) - it will use the initialized values for height, width, and color. // Constructor public Rectangle() { }

The Rectangle constructor needs two doubles. 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.

(Accessors and Mutators) 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)

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.

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.

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.

Now put it all together and compile it. 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?

To see if it works you now have to create a TestRectangle class. - which includes main. public class TestRectangle { public static void main(String[] args) {

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.

TestRectangle class To see if it worked, let’s print out the first one. Compile and run. Get it? 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());

TestRectangle class This is the rectangle object with no parameters. You can print out the height and width if you wish. 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

Now try the next rectangle using (4,40). TestRectangle class Now try the next rectangle using (4,40). 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());

Did you get it? TestRectangle class The area of the rectangle is 160.0 ÏϧÏThe perimeter is 88.0 ÏϧÏThe color is yellow

Let’s try something a little different. TestRectangle class Let’s try something a little different. Set the color for the third rectangle to “red” before you print it out. 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());

Did you get it?. TestRectangle class ÏThe area of the rectangle is 125.64999999999999 ÏϧÏThe perimeter is 78.8 ÏϧÏThe color is red ÏϧÏ

oops Try again Return