0% found this document useful (0 votes)
16 views

Method Overloading

Uploaded by

muditcoding
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Method Overloading

Uploaded by

muditcoding
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

METHOD OVERLOADING

> Method /function: A method is a module which is used simultaneously at different instances in the
program.

> Need of methods:

 To allow us to handle complex problems.


 To hide low-level details that is ambiguous and confuse.
 To reuse portions of code without retyping it.

> Syntax of methods:

<Access specifier><Return type> <method name> (parameter list)


{°//Body of the method•}

 Access specifier -public or private or protected.


 Method declared without access specifier is by default treated as public
 Return Type- Specifies the data of the value returned from the method
 Method name –Preferably related to the program
 Parameter list –Variables which receives the value passed from the arguments during
method call

For example:

> There are two components of a method

 Header (also known as method prototype)


 Body List of parameters is called signature

> Header public int add() Body return(value).The statement which sends back the value from a
method is called program return statement and issued at the end of the program which is a function
terminator.

Different ways of defining a method:

 Receiving value and returning outcome to the caller.


 Receiving values but not returning outcome to the caller and vice versa.
 Neither receiving values nor returning outcome to the caller.

> A method is invoked or called by providing the method name followed by the parameters to be
sent enclosed in parenthesis.

For example, a method with prototype as float peri(float 1, float b) can be called in the mainprogram
as: res = peri(lb).

Formal parameter:

 Parameter is a variable used with method signature that receives a value during method call.
 When the parameter values are received from the caller, then it is called formal parameter
.
METHOD OVERLOADING
Actual Parameter: If the values are passed to the method during its call from the caller, then it is
actual parameter.

> Ways to invoke methods: A method is invoked by two methods - call by value or call by reference
depending upon the ways the arguments are passed to the methods.

> Call by Value:

 Process of passing a copy of actual arguments to the formal parameters.


 Any change made in the formal will not reflect on actual argument.

> Call by Reference:

 Process of passing the reference of actual arguments to the formal parameters and any
change in the formal parameters will be reflected in the actual parameters also.

> A method terminates when either a return statement is encountered or last statement in the
method is executed.

> A method may contain several return statements but only one of them gets executed.

The method of type void does not return a value and thus it cannot be used in expressions.

> Forms of methods:

 Pure method, also called accessor.


 Impure method, also called mutator.

> Pure method or Accessor takes objects as arguments but does not modify the state of the objects.

> Impure method or mutator takes objects on an argument and changes the state of the objects.

Method overloading:

 Method with the same name but used for different purposes is called as overloaded
methods.
 It is implemented through polymorphism.
 Compiler finds the best match of the method arguments and the parameter list during
program compilation called static binding.

> Module: A module consists of single block of code.

> Method prototype: It is the first line of method definition that tells the program about the type of
value returned and number/type of arguments.

> Method signature: It refers to the number and types of arguments. It is a part of method
prototype.

> Recursive function: A function which calls by itself is called as recursive function.,
METHOD OVERLOADING
> Static method: lt belongs to a class which can be called without the object of a class. static
keyword is added before the static method name.

> Non-static method: Every method in Java is non-static that can access any static method and static
variable without using the object of the class. This method must not have static keyword in the
method name

You might also like