II Internal
II Internal
Overloading Methods
• In Java it is possible to define two or more methods within the same class that share the same
name, as long as their parameter declarations are different.
• When this is the case, the methods are said to be overloaded, and the process is referred to as
method overloading.
• When an overloaded method is invoked, Java uses the type and/or number of arguments as its
guide to determine which version of the overloaded method to actually call.
• Thus, overloaded methods must differ in the type and/or number of their parameters.
• While overloaded methods may have different return types, the return type alone is
insufficient to distinguish two versions of a method.
Method Overloading
one method having the same name, if their argument lists are different.
• It is similar to constructor overloading in Java, that allows a class to have more than one
constructor having different argument lists.
• Three ways to overload a method
class OverloadDemo {
void test() {
System.out.println("No parameters"); }
void test(int a) {
}}
class Overload {
double result;
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
}}
2. Write a java multithreading program which contains thread1 to check the input number is even or odd
second thread to square the number third thread to cube the number.
import java.util.Scanner;
this.number = number;
if (number % 2 == 0) {
else {
}
}
this.number = number;
this.number = number;
scanner.close();
t1.start();
t2.start();
t3.start();
} }
5 marks
•The finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally
block will execute even if no catch statement matches the exception.
•Any time a method is about to return to the caller from inside a try/catch block, via an uncaught exception,
the finally clause is also executed just before the method returns.
•This can be useful for closing file handles and freeing up any other resources that might have been allocated
at the beginning of a method with the intent of disposing of them before returning.
•However, each try statement requires at least one catch or a finally clause.
try {
// Simulating an exception
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("End of program"); }}
Output
End of program
2.