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

IAS 1313:

OBJECT ORIENTED PROGRAMMING

CHAPTER 4:
Methods- Introduction to Methods

Prepared by: Mrs Sivabalan 1


Content
Introduction to Method
Creating and invoking method
Passing parameter
Overloading methods VS overriding methods

Prepared by: Mrs Sivabalan 2


Introduction to methods
Methods consist of following:

Method header

Method definition

Invoke Method

Prepared by: Mrs Sivabalan 3


Method Header

Accessibility Method Formal


modifier Name Parameters

public int max (int num1, int num2)


Return value
type

Prepared by: Mrs Sivabalan 4


Exercise
Write the method header for the following:
A) a method called calculate area that return
double value with no arguments

B) a method called get salary that return


double value using number of hours worked

Prepared by: Mrs Sivabalan 5


Method Definition
public int max (int num1, int num2)
Local
{ variable

int result;
if(num1>num2) Method
result = num1; Body

else
result=num2;
return result; Return
statement
}
Prepared by: Mrs Sivabalan 6
Invoke Method
Create an object

Using the object, call/invoke/access the


method

Prepared by: Mrs Sivabalan 7


Sample of Invoke a Method
public class test
{
public static void main(String [] args)
{
number number1 = new number();
object
number1.max(5,6);
} method Actual
parameter
}
Prepared by: Mrs Sivabalan 8
Important Terms
Method signature is the
combination of the method name
and the parameter list.
The variables defined in the
method header are known as
formal parameters.
When a method is invoked, you
pass a value to the parameter. This
value is referred to as actual
parameter or argument.
9
Example:
The predictRaise() Method

Prepared by: Mrs Sivabalan 10


Local Variable
Known only within boundaries of method

Each time method executes


Variable re declared
New memory location large enough to hold type
set up and named

Prepared by: Mrs Sivabalan 11


Example: Method that Accepts Two
Arguments

Prepared by: Mrs Sivabalan 12


Creating Methods that Return Values

Primitive Type
Return either double, int, char, String
At the end MUST have return statement

Void
Return nothing
Cannot have return

Prepared by: Mrs Sivabalan 13


Example:
return statement
Primitive Type Void

Prepared by: Mrs Sivabalan 14


Type of Method

Prepared by: Mrs Sivabalan 15


Next Session
Next session on method will cover the
following:
A) CONTROL STRUCTURE
B) interactive input

Prepared by: Mrs Sivabalan 16


THANKS

Prepared by: Mrs Sivabalan 17

You might also like