/
Methods Methods

Methods - PowerPoint Presentation

giovanna-bartolotta
giovanna-bartolotta . @giovanna-bartolotta
Follow
366 views
Uploaded On 2015-11-09

Methods - PPT Presentation

Java Unit 2 Before we discuss methods we should talk briefly about objects since methods are part of objects Objects Chicken yeller new Chicken yeller is a Chicken reference ID: 187535

methods method object chicken method methods chicken object class access data parameters yeller objects type public return void cluck

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Methods" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Methods

Java Unit 2Slide2

Before we discuss

methods,

we should talk briefly about objects since methods are part of objects.

ObjectsSlide3

Chicken yeller =

new

Chicken();

yeller

is a Chicken

reference.

Instantiating ObjectsSlide4

Chicken yeller =

new

Chicken();

Instantiating Objects

yeller

0x234

0x234

Chicken

new Chicken()

creates a new Chicken Object out in memory.

yeller

stores the

location

of that new Chicken Object.Slide5

A

method usually

performs

a specific task by activating

the code inside the method

.

Methods can return a value

Return

type can be an intrinsic type

(

int

, double, etc.) or

an object (such as Chicken or String).

Example: public int getAge()

{ return age;

}

MethodsSlide6

Use the keyword

void

if a value is not to be returned by the method.

For example:public void speak()

{

System.out.println("cluck-cluck");}

Void MethodsSlide7

Methods can accept

parameters

A

parameter is an object you pass in to the method when you call

it.

Parameters

follow the same naming conventions as field

names.

For example:

public void

setAge

(

int a)

{ age = a;}

Method ParametersSlide8

A primitive data type gives the method a copy of its value. The method does not have access to the original data

.

An

object gives the method a copy of its reference that points to methods for changing object data. A method can change the data stored in an object because the method has access to the object's methods

.

We will revisit this concept later.

Passed by ValueSlide9

More than one method of the same name can be included in a class

The compiler uses the types, order, and number of parameters to determine which method to

execute

This allows us to use methods with

optional

parameters.

Method OverloadingSlide10

Methods have a signature that follows this structure.

Method Signatures

return type

name

params

code

access

public

void

speak

( )

{

System.out.println

("cluck-cluck");

}Slide11

Public

access simply means the member can be used anywhere inside or outside of the class.

Private access means that method can only be accessed within the class it is defined in.

Access

accessSlide12

Accessor

methods

(getters) return information, and are named accordingly:

getAge

()

getAccountBalance

()

Mutator

methods (setters)

modify the state of the

object and are named accordingly.

setAge()addDeposit()

Accessors

and Mutators(Getters and Setters)

This allows for Data Hiding, and important concept in OOP.Slide13

called from within the class by other methods

private

access

Helper MethodsSlide14

are special methods

that

create

an instance of your class.

always

have the same name as the

class.

never have a return type.

are

used to initialize all of the data/properties inside the

class.

ensure

that the Object is ready for use.

Constructors:Slide15

A

default

constructor:

Is provided by the compiler if you do not provide

one

Takes no parameters

Default ConstructorSlide16

Static members belong to the class and are shared by all instances of the class

.

Declared using the

static keyword

Can be accessed without an instance of the class

Static Methods Slide17

Is a self-reference to the current

object

Provided

only in instance methods, not in static methods

thisSlide18

Documenting

Methods

Methods should be carefully commented so that a reader of the program understands what task the method is performing and what data, if any, will be returned by the method

Method documentation should appear just above a method

Documentation should include a brief description of the method, any preconditions, and the

postconditionSlide19

Preconditions

and

Postconditions

The precondition states what must be true at the beginning of a method for the method to work properly.

The postcondition states what must be true after the method has executed if the method has worked properly.

Preconditions and postconditions should not state facts that the compiler will verify. They should also not refer to variables of information outside the method.

The postcondition should not state how the method accomplished its task.Slide20