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

Method Overloading

Method Overloading in Java

Uploaded by

sumathi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Method Overloading

Method Overloading in Java

Uploaded by

sumathi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Method Overloading

class OverloadDemo {
void test() { class Overload {
System.out.println("No parameters"); public static void main(String args[]) {
} OverloadDemo ob = new
OverloadDemo();
// Overload test for two integer parameters. int i= 50;
void test(int a, int b) { double result;
System.out.println("a and b: " + a + " " + b); ob.test();
} ob.test(i); //will it execute ?
ob.test(10, 20);
// overload test for a double parameter result = ob.test(123.25);
double test(double a) { System.out.println("Result of
System.out.println("double a: " + a); ob.test(123.25): " + result);
return a*a; }
} }
}

L.Sumathi ,AP/CSE ,GCT 1


Constructor Overloading
class Box {
// compute and return volume
double width;
double volume() {
double height;
return width * height * depth;
double depth;
}
// constructor used when all dimensions
}
Box(double w, double h, double d) {
width = w; class OverloadCons {
height = h; public static void main(String args[]) {
depth = d; Box mybox1 = new Box(10, 20, 15);
} Box mybox2 = new Box();
// constructor used when no dimensions Box mycube = new Box(7);
Box() { double vol;
width = -1; // use -1 to indicate vol = mybox1.volume();
height = -1; // an uninitialized System.out.println("Volume of mybox1 is " + vol);
depth = -1; // box vol = mybox2.volume();
} System.out.println("Volume of mybox2 is " + vol);
// constructor used when cube is created vol = mycube.volume();
Box(double len) { System.out.println("Volume of mycube is " + vol);
width = height = depth = len; }
} }
L.Sumathi ,AP/CSE ,GCT 2
Object as Parameters
// Objects may be passed to methods. class PassOb {
class Test { public static void main(String args[]) {
int a, b;
Test(int i, int j) { Test ob1 = new Test(100, 22);
a = i; Test ob2 = new Test(100, 22);
b = j; Test ob3 = new Test(-1, -1);
}
// return true if o is equal to the invoking System.out.println("ob1 == ob2: " +
object ob1.equals(ob2));
boolean equals(Test o) {
if(o.a == a && o.b == b) return true; System.out.println("ob1 == ob3: " +
else return false; ob1.equals(ob3));
}
} }
}

L.Sumathi ,AP/CSE ,GCT 3


Returning Object
// Returning an object. class RetOb {
class Test { public static void main(String args[]) {
int a; Test ob1 = new Test(2);
Test(int i) { Test ob2;
a = i; ob2 = ob1.incrByTen();
} System.out.println("ob1.a: " + ob1.a);
Test incrByTen() { System.out.println("ob2.a: " + ob2.a);
Test temp = new Test(a+10); ob2 = ob2.incrByTen();
return temp; System.out.println("ob2.a after second increase: “ + ob2.a);
} }
} }
Since all objects are dynamically allocated using new, you don’t need to worry about an
object going out-of-scope because the method in which it was created terminates.

The object will continue to exist as long as there is a reference to it somewhere in your
program.

When there are no references to it, the object will be reclaimed the next time garbage
collection takes place
L.Sumathi ,AP/CSE ,GCT 4
Call by Value ,Call by Reference
// Objects are passed by reference.
// Primitive types are passed by value. class Test {
class Test { int a, b;
void meth(int i, int j) { Test(int i, int j) {
i *= 2; a = i;
j /= 2; b = j;
} }
} void meth(Test o) {
class CallByValue { o.a *= 2;
public static void main(String args[]) { o.b /= 2;
Test ob = new Test(); }
int a = 15, b = 20; }
System.out.println("a and b before call: " + class CallByRef {
a + " " + b); public static void main(String args[]) {
ob.meth(a, b); Test ob = new Test(15, 20);
System.out.println("a and b after call: " + System.out.println("ob.a and ob.b before call: " +
a + " " + b); ob.a + " " + ob.b);
} ob.meth(ob);
} System.out.println("ob.a and ob.b after call: " +
ob.a + " " + ob.b);
}
} ,AP/CSE ,GCT
L.Sumathi 5
Reference
https://www.javatpoint.com/

https://nptel.ac.in/noc/courses/noc20/SEM1/noc20-cs08/

➢Herbert Schildt, “Java, The Complete Reference “,


Eleventh Edition

➢geeksforgeeks.org/differences-jdk-jre-jvm/

➢https://www.codejava.net/java-se/java-se-versions-
history

L.Sumathi ,AP/CSE ,GCT 6

You might also like