0% found this document useful (0 votes)
13 views5 pages

Understanding Java Methods and Overloading

The document provides an overview of methods in programming, detailing their definition, components, and how to define and invoke them. It explains concepts such as method signatures, return statements, static vs non-static methods, and the differences between actual and formal parameters. Additionally, it covers method overloading, ambiguous invocation, and distinctions between various types of methods, including call by value and call by reference.
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)
13 views5 pages

Understanding Java Methods and Overloading

The document provides an overview of methods in programming, detailing their definition, components, and how to define and invoke them. It explains concepts such as method signatures, return statements, static vs non-static methods, and the differences between actual and formal parameters. Additionally, it covers method overloading, ambiguous invocation, and distinctions between various types of methods, including call by value and call by reference.
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

Question 1

What is a method? Explain the various parts of a method.

Answer

A method is a named block of code within a class. It executes a defined set


of instructions when called from another part of the program. The different
parts of the method are access-modifier, return type, method-
name, parameter-list and method-body.

Question 2

What is a method signature?

Answer

The method name along with the list of parameters used in the method
prototype is known as method signature.

Question 3

How do you define and invoke a method?

Answer

The syntax of a method definition is:

[access-modifier] return type method-name (parameter-list)


{
method-body;
}

To invoke a method, write the name of the method and specify the value of
arguments of its parameter list. Below example shows the definition and
invocation of a method:

public class DisplayMessageDemo


{

public void DisplayMessage()


{
[Link]("Hello Java");
}

public void MyMessage()


{
DisplayMessage();
}
}
Question 4

What does the return statement do in a method?

Answer

Return statement sends back the value given to it from the called method
to the caller method. It also transfers the program control back to the
caller method from the called method.

Question 5

What does void signify in the method prototype?

Answer

void in method prototype means that the method does not return any
value.

Question 6

Explain the difference between actual and formal parameters.

Answer

The parameters that appear in the method definition are called formal or
dummy parameters whereas the parameters that appear in the method call
are called actual parameters.

Question 7

Explain static and non-static methods.

Answer

Static methods are created with static keyword in their method prototype
as shown below:

public static return-type method-name(parameter-list)


{
Method-body
}
Static methods can be called directly using class name but they can't
access instance variables and non-static methods of the class.

The non-static methods are created without the static keyword in their
method prototype as shown below:

public return-type method-name(parameter-list)


{
Method-body
}
An instance of the class is required to call non-static methods.
Question 8

What happens when an argument is passed by reference?

Answer

In pass by reference, the reference of the actual parameter is passed to the


formal parameter. Both actual parameter and formal parameter represent
the same memory location so any changes made to the formal parameters
get reflected in the actual parameters.

Question 9

When a method has been declared more than once in a class, how does
Java determine the overloading?

Answer

Overloading is determined based on the number and type of parameters of


the method.

Question 10

What is an ambiguous invocation? Give an example.

Answer

Sometimes there are two or more possible matches in the invocation of an


overloaded method. In such cases, the compiler cannot determine the most
specific match. This is referred to as ambiguous invocation. It causes a
compile time error. For example, the below program will cause a compile
time error due to ambiguous invocation:

public class AmbiguousDemo {

public static double max(double num1, int num2)


{
if (num1 > num2)
return num1;
else
return num2;
}

public static double max(int num1, double num2)


{
if (num1 > num2)
return num1;
else
return num2;
}

public static void main(String args[]) {


/*
* This line will cause a compile time error
* as reference to max is ambiguous
*/
[Link](max(19, 20));
}
}

Question 11

Given below are the two method definitions:

1. public static double Check(double x, double y)


2. public static double Check(int x, double y)

Which of the two methods is invoked for the following?

1. double z = Check (6, 5);


2. double z = Check (5.5, 7.4);

Answer

1. double z = Check (6, 5); invokes public static double Check(int x,


double y)
2. double z = Check (5.5, 7.4); invokes public static double
Check(double x, double y)

Question 12

What is the signature of the following method heading?

public void CoolMethod(int xx, char yy, int zz)

Answer

Signature of this method is:

CoolMethod(int xx, char yy, int zz)

Question 13

Which OOP principle implements function (method) overloading?

Answer

Polymorphism
Question 14

Differentiate between the following:

1. Call by value and Call by reference

Answer

Call by value Call by reference

Values of actual parameters are copied to Reference of actual parameters is


formal parameters. passed to formal parameters.

Changes made to formal parameters are Changes made to formal parameters are
not reflected back to actual parameters. reflected back to actual parameters.

2. Pure and Impure methods

Answer

Pure methods Impure methods

Pure methods does not modify the Impure methods change the state of received
objects. objects.

Pure methods doesn't have side


Impure methods have side effects.
effects.

3. Simple Method and Overloaded method

Answer

Simple Method Overloaded method

Simple Methods have unique In case of Overloaded methods, there are two or
names. more methods with the same name.

We can identify the method We need to examine the method's number and type
being invoked by looking at its of parameters to determine which method will be
name. invoked.

Common questions

Powered by AI

In call by value, the method receives a copy of the actual parameter's value, and changes made to the parameter within the method do not affect the original variable. In contrast, call by reference passes the memory address of the parameter, meaning that modifications within the method reflect directly on the original variable. These distinctions affect method outcomes significantly, influencing whether external data is preserved or modified during method execution .

Method invocation in Java involves calling a method by using its name and passing arguments that align with the method's parameter list, depending on its signature. The method signature, which includes the method name and parameter types, determines which method is executed in cases of overloading. The arguments provided must match the expected parameter types in number and type, as delineated by the method's signature .

Ambiguous invocation occurs when a method call could match multiple overloaded methods equally, leading to ambiguity for the compiler. For instance, in the class `AmbiguousDemo`, the methods `max(double, int)` and `max(int, double)` create an ambiguous situation when called with `max(19, 20)`, as both signatures are potential matches leading to a compile-time error. This ambiguity prevents the program from compiling and necessitates adjustment to ensure clear method calls .

The method signature consists of the method name and the list of parameters, whereas the method definition includes the full syntax: access modifier, return type, method name, parameter list, and method body. The method signature is significant in overloading because it is the basis for distinguishing between overloaded methods, which have the same name but different parameter lists .

Declaring a method as 'void' implies that the method does not return any value. This affects the method's behavior, as such methods are typically used for executing certain operations like printing to the console or modifying object states without needing to pass any information back to the caller. The absence of a return value simplifies the method's usage, as the caller does not need to handle or store any return data .

A pure method is one that does not modify the state of objects or have any observable side effects, meaning it returns the same result given the same inputs and does not alter any external state. On the other hand, an impure method can change the object's state or have side effects outside its scope, affecting program observability or behavior across different calls .

Actual parameters are the values or references provided to a method during a call, whereas formal parameters are the variables declared in the method definition that receive the actual parameters' values or references. This distinction is crucial because changes to formal parameters affect actual parameters differently based on the parameter passing mode (by value or by reference). Understanding this affects how methods interact with provided data during execution .

Java handles method overloading by allowing multiple methods with the same name but different parameter lists. The compiler determines which method to invoke based on the actual parameters provided. Ambiguous invocation arises when the compiler cannot unambiguously determine which overloaded method is the most specific match, typically due to parameters that match multiple method signatures equally well. This causes a compile-time error as seen in the example where methods `max(double, int)` and `max(int, double)` are both potential matches for the call `max(19, 20)` .

A method in object-oriented programming consists of several components: the access modifier, the return type, the method name, the parameter list, and the method body. Each part serves a specific purpose: the access modifier determines the method's visibility (e.g., public, private), the return type specifies the data type of the result returned by the method, the method name provides the identifier to call the method, the parameter list defines the input parameters required, and the method body contains the code that defines the method's behavior .

Static methods belong to the class rather than any specific instance, meaning they can be invoked directly using the class name. This grants static methods broader accessibility without needing an object reference. However, they cannot access instance variables or non-static methods directly because such access requires a specific object context. Conversely, non-static methods are dependent on object instances and can access instance fields and other non-static methods within the same object .

You might also like