Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods...

19
Method Overloading.

Transcript of Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods...

Page 1: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Method Overloading.

Page 2: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Method Overloading

• Can two methods in a class have the same

name?• Two methods in a class can have the same

name provided

– they take different numbers of arguments, or

– the type of at least one argument is different• This is called method overloading.• Why is this useful?

Page 3: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Method Overloading

• The compiler does not consider return type when differentiating methods

• We cannot declare two methods with the same signature even if they have a different return type.

• public int countRows(int number);public String countRows(int number);

• public String printString(String string)public String printString(String string, int offset)

Page 4: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Example

public class MainClass {

  public void print(int a) {    System.out.println(a);  }

  public void print(String a) {    System.out.println(a);  }

}

Page 5: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Example public class DataArtist { ... public void draw(String s) { ... } public void draw(int i) { ... } public void draw(double f) { ... } public void draw(int i, double f) { ... } }

Page 6: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Overloaded Methods

public class Circle{ public void move (int x, int y) { ... }

public void move (Point p) { ... } ...

Circle circle = new Circle(5);circle.move (50, 100);Point center = new Point(50, 100);circle.move (center);

•The compiler treats overloaded methods as completely different methods.•The compiler knows which one to call based on the number and the types of the parameters passed to the method.

Page 7: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Multiple Constructors

• Constructors can be overloaded.

• If a class has more than one constructor, they must have different numbers and/or types of parameters.

Page 8: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Examplepublic class Fraction{ private int num, denom;

public Fraction ( ) { num = 0; denom = 1; }

public Fraction (int n) { num = n; denom = 1; } public Fraction (int n, int d) { num = n; denom = d; reduce (); }

Page 9: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Calling a Constructor from a Constructor

• Constructors of a class can call each other using the keyword this — a good way to avoid duplicating code:

public class Fraction{ ...

public Fraction (int n) { this (n, 1); } ...

... public Fraction (int p, int q) { num = p; denom = q; reduce (); } ...

Page 10: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Exampleclass Sphere {// Construct a unit sphere at the originSphere() {radius = 1.0;++count; }Sphere(double x, double y, double z){this(); // Call the constructor with no argumentsxCenter = x;yCenter = y;zCenter = z;}Sphere(double theRadius, double x, double y, double z) {this(x, y, z);radius = theRadius; } // The rest of the class as before...}

Page 11: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Exampleclass OverloadDemo {

void test() {

System.out.println(“No Parameters”);

}

void test (int a) {

System.out.println(“a: “ + a);

}

void test (int a, int b) {

System.out.println(“a and b: “ + a+ “ “ +b);

}

double test(double a) {

System.out.println(“double a: ”+a);

return a*a;

}

}

Page 12: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Exampleclass Overload {

public static void main (String args[]) {

OverloadDemo ob = new OverloadDemo () ;

double result;

ob.test();

ob.test(10);

ob.test(10,20);

result = ob.test (123.25);

System.out.println(“Result of ob.test(123.2): “ + result);

}

}

Page 13: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Recursion

• A method that calls itself is described as a recursive method, and

• The process is referred to as recursion. • You can also have indirect recursion where a

method A calls another method B, which in turn calls the method A.

• Clearly you must include some logic in a recursive method so that it will eventually stop calling itself if the process is not to continue indefinitely.

Page 14: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Recursion Example

• You can write a method that will calculate integer powers of a variable—in other words, evaluate xn, or x*x...*x where x is multiplied by itself n times.

• You can use the fact that you can obtain xn by multiplying xn-1 by x.

ExampleYou can calculate 24 as 23 multiplied by 2, and you can get 23 by multiplying 22 by 2, and 22 is produced by multiplying 21, which is 2, of course, by 2.

Page 15: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Recursion Example

public class PowerCalc {public static void main(String[] args) {double x = 5.0;System.out.println(x + “ to the power 4 is “ + power(x,4));System.out.println(“7.5 to the power 5 is “ + power(7.5,5));System.out.println(“7.5 to the power 0 is “ + power(7.5,0));System.out.println(“10 to the power -2 is “ + power(10,-2));}// Raise x to the power nstatic double power(double x, int n) {if(n > 1)return x*power(x, n-1); // Recursive callelse if(n < 0)return 1.0/power(x, -n); // Negative power of xelsereturn n == 0 ? 1.0 : x; // When n is 0 return 1, otherwise x}}

Page 16: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Recursion Example

• This program will produce the following output:

• 5.0 to the power 4 is 625.0

• 7.5 to the power 5 is 23730.46875

• 7.5 to the power 0 is 1.0

• 10 to the power -2 is 0.01

Page 17: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Recursion Example

• You can see from this that the power() method is called four times in all.

• The calls cascade down through four levels until the value of n is such that it allows a value to be returned.

• The return values ripple up through the levels until you are eventually back at the top, and 625.0 is returned to the original calling point.

Page 18: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Lab Practice

• Compile & execute “Overload” & “OverloadDemo” classes

• Examine its output.

Page 19: Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.

Lab Practice• Rewrite “Employee” &

“EmployeeDemo” using overloaded constructors such as;Employee();Employee (String, int, double);Employee(String, int);

• Create 2 Employee objects & Initialize data members of Employee class for each object through constructor.

• Compile n execute the code and examine its output.