Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a java program to explain Constructor or Constructor overloading
- class Ex4 //Ex4 is Java Program Name
- {
- private int studID;
- private String studName;
- private int studAge;
- Ex4()
- {
- //Default Constructor
- studID = 100;
- studName = "New Student";
- studAge = 10;
- }
- Ex4(int num1, String str, int num2)
- {
- //Parameterized Constructor
- studID = num1;
- studName = str;
- studAge = num2;
- }
- public static void main(String args[])
- {
- //this object creation would call the default constructor
- Ex4 newobj = new Ex4();
- System.out.println("Student Name is : "+newobj.studName);
- System.out.println("Student Age is : "+newobj.studAge);
- System.out.println("Student ID is : "+newobj.studID);
- //th object creation would call the parameterized constructor student StudentData(int, String, int)
- Ex4 newobj2 = new Ex4(9,"Virajsinh",21);
- System.out.println("Student Name is : "+newobj2.studName);
- System.out.println("Student Age is : "+newobj2.studAge);
- System.out.println("Student ID is : "+newobj2.studID);
- }
- }
Add Comment
Please, Sign In to add comment