Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods...
-
Upload
earl-fisher -
Category
Documents
-
view
212 -
download
0
Transcript of Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods...
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 different numbers of arguments, or
– the type of at least one argument is different• This is called method overloading.• Why is this useful?
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)
Example
public class MainClass {
public void print(int a) { System.out.println(a); }
public void print(String a) { System.out.println(a); }
}
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) { ... } }
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.
Multiple Constructors
• Constructors can be overloaded.
• If a class has more than one constructor, they must have different numbers and/or types of parameters.
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 (); }
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 (); } ...
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...}
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;
}
}
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);
}
}
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.
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.
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}}
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
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.
Lab Practice
• Compile & execute “Overload” & “OverloadDemo” classes
• Examine its output.
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.