[go: up one dir, main page]

0% found this document useful (0 votes)
15 views9 pages

II Internal

The document discusses method overloading in Java, explaining that multiple methods can share the same name if their parameter lists differ. It provides examples of method overloading and demonstrates it through a Java program. Additionally, it covers multithreading in Java with a program that checks if a number is even or odd, squares it, and cubes it, along with an explanation of the 'finally' block in exception handling.

Uploaded by

sankaransvg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

II Internal

The document discusses method overloading in Java, explaining that multiple methods can share the same name if their parameter lists differ. It provides examples of method overloading and demonstrates it through a Java program. Additionally, it covers multithreading in Java with a program that checks if a number is even or odd, squares it, and cubes it, along with an explanation of the 'finally' block in exception handling.

Uploaded by

sankaransvg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Discuss the Overloading Methods in java with program

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.

• Method overloading is one of the ways that Java supports polymorphism.

• 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

• Method Overloading is a feature that allows a class to have more than

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

1. Number of parameters. add(int, int) add(int, int, int)

2. Data type of parameters.

add(int, int) add(int, float)

3. Sequence of Data type of parameters.

add(int, float) add(float, int)

• Invalid case of method overloading:

int add(int, int) float add(int, int)

// Demonstrate method overloading.

class OverloadDemo {

void test() {

System.out.println("No parameters"); }

// Overload test for one integer parameter.

void test(int a) {

System.out.println("a: " + a); }


// Overload test for two integer parameters.

void test(int a, int b) { System.out.println("a and b: " + a + " " + b); }

// overload test for a double parameter

double test(double a) { System.out.println("double a: " + a); return a*a;

}}

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.25): " + result);

}}
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;

class EvenOddThread extends Thread {

private int number;

public EvenOddThread(int number) {

this.number = number;

public void run() {

if (number % 2 == 0) {

System.out.println(number + " is Even");

else {

System.out.println(number + " is Odd");

}
}

class SquareThread extends Thread {

private int number;

public SquareThread(int number) {

this.number = number;

public void run() {

System.out.println("Square of " + number + " is: " + (number * number));

class CubeThread extends Thread {

private int number;

public CubeThread(int number) {

this.number = number;

public void run() {

System.out.println("Cube of " + number + " is: " + (number * number * number));


}

public class MultiThreadingExample {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

int number = scanner.nextInt();

scanner.close();

EvenOddThread t1 = new EvenOddThread(number);

SquareThread t2 = new SquareThread(number);

CubeThread t3 = new CubeThread(number);

t1.start();

t2.start();

t3.start();

} }

5 marks

1. Write note on finally


Finally creates a block of code that will be executedafter a try/catch block has completed and before the code
following the try/catch block.

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

•The finally clause is optional.

•However, each try statement requires at least one catch or a finally clause.

public class FinallyExample {

public static void main(String[] args) {

try {

System.out.println("Inside try block");

// Simulating an exception

int result = 10 / 0;

} catch (ArithmeticException e) {

System.out.println("Exception caught: " + e.getMessage());


} finally {

System.out.println("This is the finally block, which will always execute"); }

System.out.println("End of program"); }}

Output

Inside try block

Exception caught: / by zero

This is the finally block, which will always execute

End of program

2.

You might also like