Chapter 5-4: Classes and Objects in Depth
This presentation is the property of its rightful owner.
Sponsored Links
1 / 16

Introduction to methods PowerPoint PPT Presentation


  • 91 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 5-4: Classes and Objects in Depth. Introduction to methods . Method Overloading: An Introduction. Method overloading: more than one method can have the same name Two methods are said to have different formal parameter lists if both methods have:

Download Presentation

Introduction to methods

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Introduction to methods

Chapter 5-4: Classes and Objects in Depth

Introduction to methods


Method overloading an introduction

Method Overloading: An Introduction

Method overloading: more than one method can have the same name

Two methods are said to have different formal parameter lists if both methods have:

A different number of formal parameters, or

If the number of formal parameters is the same, then the data type of the formal parameters, in the order you list, must differ in at least one position

Java Programming: From Problem Analysis to Program Design, 4e


Method overloading

Method Overloading

public void methodOne(int x)

public void methodTwo(int x, double y)

public void methodThree(double y, int x)

public int methodFour(char ch, int x,

double y)

public int methodFive(char ch, int x,

String name)

These methods all have different formal parameter lists

Java Programming: From Problem Analysis to Program Design, 4e


Method overloading continued

Method Overloading (continued)

public void methodSix(int x, double y,

char ch)

public void methodSeven(int one, double u,

char firstCh)

The methods methodSix and methodSeven both have three formal parameters and the data type of the corresponding parameters is the same

These methods all have the same formal parameter lists

Java Programming: From Problem Analysis to Program Design, 4e


Method overloading continued1

Method Overloading (continued)

Method overloading: creating several methods, within a class, with the same name

The signature of a method consists of the method name and its formal parameter list

Two methods have different signatures if they have either different names or different formal parameter lists

Note that the signature of a method does not include the return type of the method

Java Programming: From Problem Analysis to Program Design, 4e


Method overloading continued2

Method Overloading (continued)

The following method headings correctly overload the method methodXYZ:

public void methodXYZ()

public void methodXYZ(int x, double y)

public void methodXYZ(double one, int y)

public void methodXYZ(int x, double y,

char ch)

Java Programming: From Problem Analysis to Program Design, 4e


Method overloading continued3

Method Overloading (continued)

public void methodABC(int x, double y)

public int methodABC(int x, double y)

Both these method headings have the same name and same formal parameter list

These method headings to overload the method methodABC are incorrect

In this case, the compiler will generate a syntax error

Notice that the return types of these method headings are different

Java Programming: From Problem Analysis to Program Design, 4e


Scope of an identifier within a class

Scope of an Identifier within a Class

Local identifier: identifier declared within a method or block, which is visible only within that method or block

Java does not allow the nesting of methods; you cannot include the definition of one method in the body of another method

Within a method or a block, an identifier must be declared before it can be used; a block is a set of statements enclosed within braces

Java Programming: From Problem Analysis to Program Design, 4e


Scope of an identifier within a class continued

Scope of an Identifier within a Class (continued)

  • A method’s definition can contain several blocks

    • The body of a loop or an if statement also form a block

  • Within a class, outside of every method definition (and every block), an identifier can be declared anywhere

Java Programming: From Problem Analysis to Program Design, 4e


Scope of an identifier within a class continued1

Scope of an Identifier within a Class (continued)

Within a method, an identifier used to name a variable in the outer block of the method cannot be used to name any other variable in an inner block of the method

For example, in the method definition on the next slide, the second declaration of the variable x is illegal

Java Programming: From Problem Analysis to Program Design, 4e


Scope of an identifier within a class continued2

Scope of an Identifier within a Class (continued)

public static void illegalIdentifierDeclaration()

{

int x;

//block

{

double x; //illegal declaration,

//x is already declared

...

}

}

Java Programming: From Problem Analysis to Program Design, 4e


Scope rules

Scope Rules

Scope rules of an identifier declared within a class and accessed within a method (block) of the class

An identifier, say X, declared within a method (block) is accessible:

Only within the block from the point at which it is declared until the end of the block

By those blocks that are nested within that block

Java Programming: From Problem Analysis to Program Design, 4e


Scope rules continued

Scope Rules (continued)

  • Suppose X is an identifier declared within a class and outside of every method’s definition (block)

    • If X is declared without the reserved word static (such as a named constant or a method name), then it cannot be accessed in a static method

    • If X is declared with the reserved word static (such as a named constant or a method name), then it can be accessed within a method (block), provided the method (block) does not have any other identifier named X

Java Programming: From Problem Analysis to Program Design, 4e


Scope rules continued1

Example 7-11

public classScopeRules

{

static final double rate = 10.50;

static int z;

static double t;

public static void main(String[]args)

{

intnum;

doublex, z;

charch;

//...

}

public static void one(int x, char y)

{

//...

}

Scope Rules (continued)

public static int w;

public static void two(int one,int z)

{

charch;

int a;

//block three

{

int x = 12;

//...

} //end block three

//...

}

}

Java Programming: From Problem Analysis to Program Design, 4e


Scope rules demonstrated

Scope Rules: Demonstrated

Java Programming: From Problem Analysis to Program Design, 4e


Scope rules demonstrated continued

Scope Rules: Demonstrated (continued)

Java Programming: From Problem Analysis to Program Design, 4e


  • Login