Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

3-8-05

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 22

Classes and Methods

(Cont’d)
1
(Some slides omitted or modified.)
Method Signature
 The signature of a method consists of the
method’s name, together with the number
and type(s) of its parameters in their given
order

 Method overloading is the use of a method


name more than once, each time with
different signatures
2
Method Overloading Means
 Several methods of the same name are
defined with different sets of parameters
(based on number of parameters, types of
parameters, or order of parameters)
 Multiple methods can share the same name
if at least one of these conditions is met:
 1. They have different NUMBERS of parameters
 2. The parameters are different datatypes

3
Method Overloading Examples
 These methods are overloaded:
public void someMethod (int a, int b) {….}
public void someMethod (int y) {..}

 These methods are overloaded :


public void someMethod (int a, int b) {….}
public void someMethod (int a, double b) {..}

 These methods are NOT overloaded :


public void someMethod (int a, int b) {….}
public double someMethod (int a, int b) {..}
4
Constructors
The default constructor has no parameters and no
statements in the body:
public Student ( ) {
}
 If no constructor is defined for a class, Java
compiler will automatically add the default
 Do not rely on default; always define your own

5
Constructor Overloading Examples
 Multiple constructors are often defined for
an object-creating class.
 Overloaded Constructor with one
parameter:
public Student (String inputName ) {..}
 Overloaded Constructor with two
parameters:
public Student (String inputName, int inputID )
{..}
6
Object State
 The object’s state is the current set of values that it
contains
 An object is instantiated with an initial state. If any
of its methods can subsequently change its state,
the object is said to be mutable
 If there are no methods that can change an
object’s state, it is said to be immutable (Ex.:
String object)
 Methods that change an object’s state are called
mutators or transformers

7
Chapter 6
Object-Oriented Software Design and
Implementation
8
Software Design Strategies
FUNCTIONAL OBJECT-ORIENTED
DECOMPOSITION DESIGN

The problem is divided into The solution is expressed


more easily handled in terms of objects
subproblems, the solutions (self-contained entities
of which together create a composed of data and
solution to the overall operations on that data) that
problem. Produces a interact by sending messages
hierarchy of tasks. to one another. Produces a
hierarchy of objects.

9
Object-Oriented Design (OOD)
•Focus is on the entities (objects) in a problem

•Begins by identifying the classes of objects in the


problem, and choosing appropriate operations on
those objects

•Programs are collections of objects that


communicate with (send messages to) each other

•Data plays a leading role; algorithms are used to


implement operations on the objects and to enable
interaction of objects with each other
10
OOD good with large software projects
 Objects within a program often model real-life
objects in the problem to be solved

 The OOD concept of inheritance allows the


customization of an existing class to meet
particular needs. This can reduce the time and
effort needed to design, implement, and
maintain large systems

11
Object-Oriented Design Process
 Three steps in the process
 Identify an initial set of object classes
that seem relevant to the problem
– nouns often represent objects
– verbs often represent actions
 Filter the list, eliminating duplicates
 Identify the responsibilities for the
reduced list of objects

12
Identify Possible Classes
 Brainstorming (by a team)
 Identify objects
 Propose classes
 Write on a blackboard
 Keep going around until no one can
think of any more objects
 No ideas are rejected

13
Filter List of Possible Classes
 Eliminate duplicates
 Decide if the classes really do represent
objects in the problem
 Look for classes that are related
 Over looked classes may emerge
 For each class that survives the filtering
stage, create a CRC card
14
Blank CRC Card
Class Name: Superclass: Subclasses:

Responsibilities Collaborations

15
Definitions
 Responsibilities An action that an
implementation of an object must be
capable of performing
 Collaboration An interaction between
objects in which one object requests that
another object carry out one of its
responsibilities

16
Determine Responsibilities
 Initial responsibilities
 Know and return the states of the object
 Called Knowledge Responsibilities
 Action responsibilities Use scenario
walk-throughs, a role playing technique,
 to explore the actions of a class
 to explore the interactions of classes

17
Inheritance
 Inheritance A mechanism by which one
class acquires (inherits) the properties
(both data fields and methods) of another
class.
 Enables us to define a new class by adapting
the definition of an existing class
 Superclass The class being inherited from
 Derived class The class that inherits
 The derived class is specialized by adding
properties specific to it 18
Address object inheritance hierarchy
Object

Address

HomeAddress CompanyAddress

WorkAddress BoxAddress

19
Package Syntax
Compilation Unit

package Identifier ;

ImportDeclaration . . .

ClassDeclaration . . .

20
Package Do’s and Don’t’s
 A compilation unit can have only one
public class
 Many compilation units can be in a
package
 “No modifier” specifies package access
 Any field or method with package access
can be accessed by any member of the
package
21
Package Example
 package addressBook
 Members:
 class Address
 class Entry
 Imported by
 class AddressDr
 All of the variables have package
access
22

You might also like