+ All Categories
Home > Documents > Classes and Methods (Cont’d) (Some slides omitted or modified.)

Classes and Methods (Cont’d) (Some slides omitted or modified.)

Date post: 06-Jan-2016
Category:
Upload: will
View: 15 times
Download: 0 times
Share this document with a friend
Description:
Classes and Methods (Cont’d) (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 - PowerPoint PPT Presentation
22
1 Classes and Methods (Cont’d) (Some slides omitted or modified.)
Transcript
Page 1: Classes and Methods (Cont’d) (Some slides omitted or modified.)

1

Classes and Methods

(Cont’d)(Some slides omitted or modified.)

Page 2: Classes and Methods (Cont’d) (Some slides omitted or modified.)

2

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

Page 3: Classes and Methods (Cont’d) (Some slides omitted or modified.)

3

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

Page 4: Classes and Methods (Cont’d) (Some slides omitted or modified.)

4

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) {..}

Page 5: Classes and Methods (Cont’d) (Some slides omitted or modified.)

5

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

Page 6: Classes and Methods (Cont’d) (Some slides omitted or modified.)

6

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 )

{..}

Page 7: Classes and Methods (Cont’d) (Some slides omitted or modified.)

7

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

Page 8: Classes and Methods (Cont’d) (Some slides omitted or modified.)

8

Chapter 6

Object-Oriented Software Design and Implementation

Page 9: Classes and Methods (Cont’d) (Some slides omitted or modified.)

9

Software Design Strategies FUNCTIONAL OBJECT-ORIENTED

DECOMPOSITION DESIGN

The solution is expressed

in terms of objects

(self-contained entities

composed of data and

operations on that data) that

interact by sending messages

to one another. Produces a

hierarchy of objects.

The problem is divided into

more easily handled

subproblems, the solutions

of which together create a

solution to the overall

problem. Produces a

hierarchy of tasks.

Page 10: Classes and Methods (Cont’d) (Some slides omitted or modified.)

10

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

Page 11: Classes and Methods (Cont’d) (Some slides omitted or modified.)

11

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

Page 12: Classes and Methods (Cont’d) (Some slides omitted or modified.)

12

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

Page 13: Classes and Methods (Cont’d) (Some slides omitted or modified.)

13

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

Page 14: Classes and Methods (Cont’d) (Some slides omitted or modified.)

14

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

Page 15: Classes and Methods (Cont’d) (Some slides omitted or modified.)

15

Blank CRC Card

Class Name: Superclass: Subclasses:

Responsibilities Collaborations

Page 16: Classes and Methods (Cont’d) (Some slides omitted or modified.)

16

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

Page 17: Classes and Methods (Cont’d) (Some slides omitted or modified.)

17

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

Page 18: Classes and Methods (Cont’d) (Some slides omitted or modified.)

18

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

Page 19: Classes and Methods (Cont’d) (Some slides omitted or modified.)

19

Address object inheritance hierarchy

Object

CompanyAddressHomeAddress

BoxAddressWorkAddress

Address

Page 20: Classes and Methods (Cont’d) (Some slides omitted or modified.)

20

Package Syntax Compilation Unit

package Identifier ;

ImportDeclaration . . .

ClassDeclaration . . .

Page 21: Classes and Methods (Cont’d) (Some slides omitted or modified.)

21

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

Page 22: Classes and Methods (Cont’d) (Some slides omitted or modified.)

22

Package Example

package addressBook Members:

class Address class Entry

Imported by class AddressDr

All of the variables have package access


Recommended