QN035. Write a class to accept and print student information using overloaded constructors.
1. Objective
To accept and print student information using overloaded constructors.
2. Theory
Constructor overloading is a concept of having more than one constructor with different parameter lists, in such a way so that each constructor performs a different task.
3. Source Code
import java.util.Scanner;
public class Qn035{
static String name, email;
static int rollNo;
static long phoneNo;
//creating three arguments constructor
Qn035(String n, int r, String e){
name = n;
rollNo = r;
email = e;
}
//creating four arguments constructor
Qn035(String n, int r, String e, long p){
name = n;
rollNo = r;
email = e;
phoneNo = p;
}
void infoDisplay(){
System.out.println("\nStudent Details");
System.out.println("Name: "+ name);
System.out.println("Roll No.: "+ rollNo);
System.out.println("Email: "+ email);
System.out.println("Phone Number: "+ phoneNo);
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter your Details");
System.out.print("What is your name? ");
name = input.nextLine();
System.out.print("Enter your roll number: ");
rollNo = Integer.parseInt(input.nextLine());
System.out.print("Enter your email: ");
email = input.nextLine();
System.out.print("Enter your phone Number: ");
phoneNo = Long.parseLong(input.nextLine());
Qn035 s1 = new Qn035(name,rollNo,email,phoneNo);
s1.infoDisplay();
//Qn035 s2 = new Qn035(name,rollNo,email);
//s2.infoDisplay();
}
}
4. Output
Enter your Details
What is your name? Sajan Kc
Enter your roll number: 10
Enter your email: sazankce@gmail.com
Enter your phone number: 9843337779
Student Details
Name: Sajan Kc
Roll No: 10
Email: sazankce@gmail.com
Phone Number: 9843337779
5. Conclusion
In this way, I would like to conclude that by creating multiple method under same name but with different numbers of argument we can achieve the constructor overloading.