Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Method Overloading.

2 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?

3 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)

4 Example public class MainClass { public void print(int a) { System.out.println(a); } public void print(String a) { System.out.println(a); } }

5 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) {... } }

6 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.

7 Multiple Constructors Constructors can be overloaded. If a class has more than one constructor, they must have different numbers and/or types of parameters.

8 Example public 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 (); }

9 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 (); }...

10 Example class Sphere { // Construct a unit sphere at the origin Sphere() { radius = 1.0; ++count; } Sphere(double x, double y, double z) { this(); // Call the constructor with no arguments xCenter = 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... }

11 Example class 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; }

12 Example class 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); }

13 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.

14 Recursion Example You can write a method that will calculate integer powers of a variable—in other words, evaluate x n, or x*x...*x where x is multiplied by itself n times. You can use the fact that you can obtain x n by multiplying x n -1 by x. Example You can calculate 2 4 as 2 3 multiplied by 2, and you can get 2 3 by multiplying 2 2 by 2, and 2 2 is produced by multiplying 2 1, which is 2, of course, by 2.

15 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 n static double power(double x, int n) { if(n > 1) return x*power(x, n-1); // Recursive call else if(n < 0) return 1.0/power(x, -n); // Negative power of x else return n == 0 ? 1.0 : x; // When n is 0 return 1, otherwise x }

16 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

17 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.

18 Lab Practice Compile & execute “Overload” & “OverloadDemo” classes Examine its output.

19 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.


Download ppt "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."

Similar presentations


Ads by Google