OOPS with Java Lab B.
Tech (DS) || Sec- ‘D’
Experiment – 3
Object:- Write a java program for Method overloading and Constructor overloading.
Program:-
Method overloading:-
import [Link].*;
class MethodOverloading {
static int add(int a, int b)
{
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
public static void main(String args[])
{
[Link]("add() with 2 parameters");
[Link](add(7, 3));
[Link]("add() with 3 parameters");
[Link](add(7, 8, 5));
}
}
Output:-
6|Page Roll No:2301201540052
OOPS with Java Lab [Link] (DS) || Sec- ‘D’
Constructor overloading:-
public class Student {
// Instance variables of the class
int id;
String name;
Student(){
[Link]("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
public static void main(String[] args) {
// Object creation
Student s = new Student();
[Link]("\nDefault Constructor values: \n");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
[Link]("\nParameterized Constructor values: \n");
Student student = new Student(28, "Divyansh");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
}
}
Output:-
7|Page Roll No:2301201540052
OOPS with Java Lab [Link] (DS) || Sec- ‘D’
8|Page Roll No:2301201540052