1.
Write a java program for Method overloading and
Constructor overloading.
Solution:
Java Code:
public class OverloadingExample {
// Method overloading
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
// Constructor overloading
private int value;
public OverloadingExample() {
value = 0;
}
public OverloadingExample(int val) {
value = val;
}
public void display() {
System.out.println("Value: " + value);
}
public static void main(String[] args) {
OverloadingExample obj = new
OverloadingExample();
obj.display();
OverloadingExample obj2 = new
OverloadingExample(5);
obj2.display();
}
}
Output:
2. Write a java program to display the employee details
using Scanner class.
Solution:
Java Code:
import java.util.Scanner;
public class EmployeeDetails {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter employee name:");
String name = scanner.nextLine();
System.out.println("Enter employee age:");
int age = scanner.nextInt();
System.out.println("Enter employee ID:");
int id = scanner.nextInt();
System.out.println("\nEmployee Details:");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("ID: " + id);
scanner.close();
}
}
Output:
3. Write a java program to implement Interface using
extends keyword.
Solution:
Java Code:
interface MyInterface {
void display();
}
class InterfaceImplementation implements MyInterface {
public void display() {
System.out.println("Implementation of display method
from MyInterface");
}
}
public class InterfaceExtendsExample extends
InterfaceImplementation {
public static void main(String[] args) {
InterfaceExtendsExample obj = new
InterfaceExtendsExample();
obj.display();
}
}
Output:
Implementation of display method from MyInterface