0% found this document useful (0 votes)
19 views28 pages

Object As Parameter Returning Object From A Function Function Overloading

The document discusses several topics related to objects and functions in Java: 1. Object as Parameter - Objects can be passed to member functions as parameters by reference rather than making a copy. The reference of the object is passed by value. 2. Returning Object from a function - A function can return an object by returning the reference of the object rather than the object itself. 3. Function Overloading - Having two or more functions with the same name but different parameters allows functions to be overloaded. The document then discusses variable-length arguments in Java using the varargs keyword to simplify methods that take a variable number of arguments.

Uploaded by

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

Object As Parameter Returning Object From A Function Function Overloading

The document discusses several topics related to objects and functions in Java: 1. Object as Parameter - Objects can be passed to member functions as parameters by reference rather than making a copy. The reference of the object is passed by value. 2. Returning Object from a function - A function can return an object by returning the reference of the object rather than the object itself. 3. Function Overloading - Having two or more functions with the same name but different parameters allows functions to be overloaded. The document then discusses variable-length arguments in Java using the varargs keyword to simplify methods that take a variable number of arguments.

Uploaded by

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

Object as Parameter

Returning Object from a function


Function Overloading
Object as Parameter
 Object can be passed to the member
functions as parameter.
 Objects are passed by reference.
 No separate copy is made of the argument
copy.
 The reference of the object is passed by
value.
 In parameter passing reference of the
object is passed to the called function.
Passing Object as Parameter
1. class Example
1. Class demo
2. { 2. {
3. int x; 3. public static void main (String
args[])
4. Example(int a) 4. {
5. { 5. Example ob1=new Example(10);
6. x=a; 6. Example ob2=new Example(0);
7. Ob2.copy(ob1); // passing ob2 as
7. } parameter.

8. void copy (Example ob) 8. Ob1.get();


9. Ob2.get();
9. { 10. }
10.x=ob.x; 11. }
11.} 12. Output:
12. Void get() 13. 10
13. { 14. 10
14. System.out.println(“x=“ + x);
15. }
16. }
Returning Object from a function

 Object can be returned from a function.


 The return type of the function should
describe the return data type as the class
name.
 In java objects are not returned.
 Instead the reference of the object is
returned.
Returning Object 1. Class demo
2. {
1. Class Example
3. public static void main (String as[])
2. {
4. {
3. int x;
5. Example ob1=new Example(10);
4. Example(int a)
6. Example ob2=new Example(20);
5. { 7. Example ob3;
6. x=a; 8. Ob3=ob1.add(ob2);
7. } 9. Ob1.get();
8. Example Add (Example ob) 10. Ob2.get();
9. { 11. Ob3.get();
10. Example temp=new Example(0); 12. }
11. Temp.x=x+ob.x; 13. }
12. Return temp; 14. Output:
13. } 15. 10
14. Void get() 16. 20
15. { 17. 30
16. System.out.println(“x=“ + x);
17. }
18. }
Function Overloading.

 Two or more than two functions same


name but different number of arguments
or same number of arguments with
different data types is called function
overloading.
Function overloading
Class demo
1. Class A {
2. { Public static void main(String as[])
3. Void set() {
4. { A ob1=new A();
5. System.out.println(“no Ob1.set();
argument set”); Ob1.set(2);
6. } Ob1.set(2,8.3f);
7. Void set(int x)
8. { }
9. Sytem.out.println(“one }
argument set”);
Output:
10. }
1. System.out.println(“no argument
11. Void set(int x, float y) set”)
12. { 2. Sytem.out.println(“one argument
13. Sytem.out.println(“two set”)
argument set”); 3. Sytem.out.println(“two argument
set”)
14. }
15. }
Variable-length arguments

 Beginning with JDK 5, Java has included a


feature that simplifies the creation of
methods that need to take a variable number
of arguments.
 This feature is called varargs and it is short
for variable-length arguments.
 A method that takes a variable number of
arguments is called a variable-arity method,
or simply a varargs method.
Variable-length arguments

 For example, a method that opens an


Internet connection might take a user
name, password, filename, protocol, and so
on, but should be able to operate with
default values if some of this information is
not provided.
 In this situation, it would be convenient to
pass only the arguments to which the
defaults did not apply.
Variable-length arguments

 Another example is the printf( ) method


that is part of Java’s I/O library. It takes a
variable number of arguments, which it
formats and then outputs.
 Avariable-length argument is specified by
three periods (...). For example, here is
how vaTest( ) is written using a vararg:
void vaTest(int ... v) { }
Variable-length arguments

 This syntax tells the compiler that


vaTest( ) can be called with zero or more
arguments.
 As a result, v is implicitly declared as an
array of type int[ ].
 Thus, inside vaTest( ), v is accessed using
the normal array syntax.
Example of Variable-length
arguments
1. // Demonstrate variable-length arguments.
2. class VarArgs {
3. static void vaTest(int ... v) {
4. System.out.print("Number of args: " + v.length + " Contents: ");
5. for(int x : v)
6. System.out.print(x + " ");
7. System.out.println(); }
8. public static void main(String args[]) {
9. // Notice how vaTest() can be called with a
10. // variable number of arguments.
11. vaTest(10); // 1 arg
12. vaTest(1, 2, 3); // 3 args
13. vaTest(); // no args } }
Variable-length arguments

 There are two important things to notice


about this program.
 First, as explained, inside vaTest( ), v is
operated on as an array.
 This is because v is an array.
 The ... syntax simply tells the compiler that a
variable number of arguments will be used,
and that these arguments will be stored in
the array referred to by v.
Variable-length arguments

 Second, in main( ), vaTest( ) is called with


different numbers of arguments, including
no arguments at all.
 The arguments are automatically put in an
array and passed to v.
 In the case of no arguments, the length of
the array is zero.
Variable-length arguments

 Amethod can have “normal” parameters


along with a variable-length parameter.
 However, the variable-length parameter
must be the last parameter declared by
the method. For example,
 this method declaration is perfectly
acceptable:
 int xyz(int a, int b, double c, int ... vals) {}
Variable-length arguments
 In this case, the first three arguments used in a call to
xyz( ) are matched to the first three parameters.
 Then, any remaining arguments are assumed to
belong to vals.
 Remember, the varargs parameter must be last.
 For example, the following declaration
 is incorrect:
 int doIt(int a, int b, double c, int ... vals, boolean
stopFlag) { // Error!
Variable-length arguments

 For example, the following declaration is


incorrect:
int xyz(int a, int b, double c, int ... vals, boolean
stopFlag) { } // Error!
 Here, there is an attempt to declare a
regular parameter after the varargs
parameter, which is illegal.
Variable-length arguments

 There is one more restriction to be aware


of:
there must be only one varargs parameter in
the signature of method.
 For example, this declaration is also
invalid:
int doIt(int a, int b, double c, int ... vals,
double ... morevals) { } // Error!
 The attempt to declare the second varargs
parameter is illegal.
Example 2 of Variable-length
arguments
 Here is a reworked version of the 1. public static void main(String args[])
vaTest( ) method that takes a 2. {
regular argument and a variable- 3. vaTest("One vararg: ", 10);
4. vaTest("Three varargs: ", 1, 2, 3);
length argument:
5. vaTest("No varargs: ");
 // Use varargs with standard 6. }
arguments. 7. }
1. class VarArgs2 {
2. // Here, msg is a normal parameter and v is a 8. The output from this program is shown here:
3. // varargs parameter. 9. One vararg: 1 Contents: 10
4. static void vaTest(String msg, int ... v) { 10. Three varargs: 3 Contents: 1 2 3
5. System.out.print(msg + v.length + 11. No varargs: 0 Contents:
6. " Contents: ");
7. for(int x : v)
8. System.out.print(x + " ");
9. System.out.println();
10. }
Overloading Vararg Methods

 You can overload a method that takes a


variable-length argument.
 For example, the following program
overloads vaTest( ) three times:
Example of overloading vararg
1. // Varargs and overloading.
2. class VarArgs3 {
3. static void vaTest(int ... v) {
4. System.out.print("vaTest(int ...): " + "Number of args: " + v.length + " Contents: ");
5. for(int x : v)
6. System.out.print(x + " "); }
7. static void vaTest(boolean ... v) {
8. System.out.print("vaTest(boolean ...) " + "Number of args: " + v.length + " Contents: ");
9. for(boolean x : v)
10. System.out.print(x + " ");
11. System.out.println(); }
12. static void vaTest(String msg, int ... v) {
13. System.out.print("vaTest(String, int ...): " + msg + v.length + " Contents: ");
14. for(int x : v)
15. System.out.println(x + " ");
16. }
Example continued……………..
1. public static void main(String args[])
2. {
3. vaTest(1, 2, 3);
4. vaTest("Testing: ", 10, 20);
5. vaTest(true, false, false);
6. }
7. }

 The output produced by this program is shown here:


 vaTest(int ...): Number of args: 3 Contents: 1 2 3
 vaTest(String, int ...): Testing: 2 Contents: 10 20
 vaTest(boolean ...) Number of args: 3 Contents: true false false
Varargs and Ambiguity

 Somewhat unexpected errors can result


when overloading a method that takes a
variable-length argument.
 These errors involve ambiguity because it
is possible to create an ambiguous call to
an overloaded varargs method.
 For example, consider the following
program:
Ambiguity example
 class VarArgs4 {
 static void vaTest(int ... v) {
 System.out.print("vaTest(int ...): " + "Number of args: " + v.length + " Contents: ");
 for(int x : v)
 System.out.print(x + " ");
 System.out.println(); }
 static void vaTest(boolean ... v) {
 System.out.print("vaTest(boolean ...) " + "Number of args: " + v.length + " Contents: ");
 for(boolean x : v)
 System.out.print(x + " ");
 System.out.println(); }
 public static void main(String args[]) {
 vaTest(1, 2, 3); // OK
 vaTest(true, false, false); // OK
 vaTest(); // Error: Ambiguous!
 }}
Varargs and Ambiguity
 In this program, the overloading of vaTest( ) is perfectly
correct.
 However, this program will not compile because of the
following call:
 vaTest(); // Error: Ambiguous!
 Because the vararg parameter can be empty, this call
could be translated into a call to:
 vaTest(int ...) or
 vaTest(boolean ...).
 Both are equally valid.
 Thus, the call is inherently ambiguous.
Varargs and Ambiguity
 Here is another example of ambiguity.
 The following overloaded versions of vaTest( ) are
inherently ambiguous even though one takes a normal
parameter:
 static void vaTest(int ... v) { }// ...
 static void vaTest(int n, int ... v) { }// ...
 Although the parameter lists of vaTest( ) differ, there is
no way for the compiler to resolve the following call:
 vaTest(1)
Varargs and Ambiguity
 Does this translate into a call to
vaTest(int ...), with one varargs argument,
or into a call to vaTest(int, int ...) with no
varargs arguments?
 There is no way for the compiler to answer
 this question. Thus, the situation is
ambiguous.
Varargs and Ambiguity

 Because of ambiguity errors like those just


shown, sometimes you will need to decline
overloading and simply use two different
method names.

You might also like