Virajsinh

Java_04

Jan 28th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. //Write a java program to explain Constructor or Constructor overloading
  2.  
  3. class Ex4 //Ex4 is Java Program Name
  4. {
  5.     private int studID;
  6.     private String studName;
  7.     private int studAge;
  8.    
  9.     Ex4()
  10.     {
  11.         //Default Constructor
  12.         studID = 100;
  13.         studName = "New Student";
  14.         studAge = 10;
  15.     }
  16.    
  17.     Ex4(int num1, String str, int num2)
  18.     {
  19.         //Parameterized Constructor
  20.         studID = num1;
  21.         studName = str;
  22.         studAge = num2;
  23.     }
  24.    
  25.     public static void main(String args[])
  26.     {
  27.         //this object creation would call the default constructor
  28.         Ex4 newobj = new Ex4();
  29.         System.out.println("Student Name is : "+newobj.studName);
  30.         System.out.println("Student Age is : "+newobj.studAge);
  31.         System.out.println("Student ID is : "+newobj.studID);
  32.        
  33.         //th object creation would call the parameterized constructor student StudentData(int, String, int)
  34.         Ex4 newobj2 = new Ex4(9,"Virajsinh",21);
  35.         System.out.println("Student Name is : "+newobj2.studName);
  36.         System.out.println("Student Age is : "+newobj2.studAge);
  37.         System.out.println("Student ID is : "+newobj2.studID);
  38.     }
  39. }
Add Comment
Please, Sign In to add comment