Program 2 a)
Write a java program to implement method overloading and constructors overloading
import [Link];
/** * Demonstrates Method Overloading and Constructor Overloading in Java */
public class OverloadingDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
// Demonstrating Constructor Overloading
Calculator calc1 = new Calculator(); // Default constructor
Calculator calc2 = new Calculator(5); // One parameter
Calculator calc3 = new Calculator(10, 20); // Two parameters
// Demonstrating Method Overloading
[Link]("\nMethod Overloading Examples:");
[Link]("Sum using object values (calc3): " + [Link]());
[Link]("Sum of 7 and 8: " + [Link](7, 8));
[Link]("Sum of 1, 2, and 3: " + [Link](1, 2, 3));
[Link]("Sum of 5.5 and 4.5: " + [Link](5.5, 4.5));
} catch (Exception e) {
[Link]("Error: Invalid input or operation.");
} finally {
[Link]();
} } }
class Calculator {
private int num1;
private int num2;
// Default constructor (no parameters)
public Calculator() {
this.num1 = 0;
this.num2 = 0;
[Link]("Default constructor called: num1 = 0, num2 = 0");
}
// Constructor with one parameter
public Calculator(int num1) {
this.num1 = num1;
this.num2 = 0;
[Link]("Constructor with one parameter called: num1 = " + num1 + ", num2 = 0");
}
// Constructor with two parameters
public Calculator(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
[Link]("Constructor with two parameters called: num1 = " + num1 + ", num2 = " + num2);
}
// Method Overloading: add() with no parameters
public int add() {
return num1 + num2;
}
// Method Overloading: add() with two int parameters
public int add(int a, int b) {
return a + b;
}
// Method Overloading: add() with three int parameters
public int add(int a, int b, int c) {
return a + b + c;
}
// Method Overloading: add() with double parameters
public double add(double a, double b) {
return a + b;
}
}
o/p: Default constructor called: num1 = 0, num2 = 0
Constructor with one parameter called: num1 = 5, num2 = 0
Constructor with two parameters called: num1 = 10, num2 = 20
Method Overloading Examples:
Sum using object values (calc3): 30
Sum of 7 and 8: 15
Sum of 1, 2, and 3: 6
Sum of 5.5 and 4.5: 10.0
Program 2 b)
Write a java program to implement method overriding.
// Demonstration of Method Overriding in Java
import [Link];
public class MethodOverridingDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Choose an animal (dog/cat): ");
String choice = [Link]().trim().toLowerCase();
Animal animal; // Reference of parent type
// Input validation and object creation
switch (choice) {
case "dog":
animal = new Dog();
break;
case "cat":
animal = new Cat();
break;
default:
[Link]("Invalid choice. Defaulting to generic Animal.");
animal = new Animal();
}
// Calls the overridden method based on the actual object type
[Link]();
[Link]();
}
}
// Parent class
class Animal {
// Method to be overridden
public void sound() {
[Link]("Animal makes a sound");
}
}
// Child class overriding the sound() method
class Dog extends Animal {
@Override
public void sound() {
[Link]("Dog barks");
}
}
// Another child class overriding the sound() method
class Cat extends Animal {
@Override
public void sound() {
[Link]("Cat meows");
}
}
o/p: Choose an animal (dog/cat):
dog
Dog barks