Chapter 6.4

7
DEFINING AND CREATING USER - DEFINED CLASS Chapter 6.4:

Transcript of Chapter 6.4

Page 1: Chapter 6.4

DEFINING AND CREATING USER-

DEFINED CLASS

Chapter 6.4:

Page 2: Chapter 6.4

Writing Classes

The programs we’ve written in previous examples

have used classes defined in the Java standard

class library

Now we will begin to design programs that rely on

classes that we write ourselves

True object-oriented programming is based on

defining classes that represent objects with well-

defined characteristics and functionality

Page 3: Chapter 6.4

Graphical Representation of a Class

The notation we used here is based on the industry standard notation

called UML, which stands for Unified Modeling Language.

A UML Class Diagram

Class Name

Variables

Method

Type of data

Access

Type of

return

value

A class can

contain data

declarations and

method

declarations

Page 4: Chapter 6.4

Object Instantiation

class

object

Page 5: Chapter 6.4

Object Design Questions

What role will the object perform?

What data or information will it need?

Look for nouns.

Which actions will it take?

Look for verbs.

What interface will it present to other objects?

These are public methods.

What information will it hide from other objects?

These are private.

Page 6: Chapter 6.4

Design Specification for a Rectangle

Class Name: Rectangle

Role: To represent a geometric rectangle

States (Information or instance variables) Length: A variable to store rectangle’s length (private)

Width: A variable to store rectangle's width (private)

Behaviors (public methods)- Rectangle(): A constructor method to set a

rectangle’s length and width

- calculateArea(): A method to calculate a rectangle’s area

Page 7: Chapter 6.4

UML Design Specification

UML Class Diagram

Class Name

What data does it need?

What behaviors

will it perform?Public

methods

Hiddeninformation

Instance variables -- memory locations

used for storing the information needed.

Methods -- blocks of code used to

perform a specific task.