Constructor Overloading in Java

Constructor Overloading in Java

Constructor Overloading in java  is allowing a class to have more than one constructor with each taking different parameters.

Example 

public class Student {
    String name;
    int studentID;
    String gender;
    
    Student(String s,int id)
    {
        name=s;
        studentID=id;
    }
    Student(String s,String g,int id)
    {
    name=s;
    gender=g;
        studentID=id;
    }
    
   void showDetails()
   {
        System.out.println("Student name is " +name);
         System.out.println("Student ID  is " +studentID);
         System.out.println("Student gender is " +gender);
   }
    public static void main(String args[])
    {
    Student s =new Student("Eric Mutua",222221);
    s.showDetails();
    Student s2=new Student("Jane Kate","Female",232222);
    s2.showDetails();
   
    }
    
}


Output:

Student name is Eric Mutua
Student ID  is 222221
Student gender is null
Student name is Jane Kate
Student ID  is 232222
Student gender is Female
BUILD SUCCESSFUL (total time: 0 seconds)

Java Copy Constructor

There are three ways of copying java object values into another java object.These are:-
  •  By constructor 
  • By assigning the values of one object into another 
  • By clone() method of Object class
Below example shows how to copy from one object to another using Java Constructor.
public class Student {
    String name;
    int studentID;
    
    
    Student(String s,int id)
    {
        name=s;
        studentID=id;
    }
    Student(Student s)
    {
    name=s.name;
  
        studentID=s.studentID;
    }
    
   void showDetails()
   {
        System.out.println("Student name is " +name);
         System.out.println("Student ID  is " +studentID);
   
   }
    public static void main(String args[])
    {
    Student s =new Student("Eric Mutua",222221);
    s.showDetails();
    Student s2=new Student(s);
    s2.showDetails();
   
    }
    
}


Output

run:
Student name is Eric Mutua
Student ID  is 222221
Student name is Eric Mutua
Student ID  is 222221
BUILD SUCCESSFUL (total time: 0 seconds)

Copying values without constructor

Its possible to copy values of one object  and assign them to another object 

Example:

public class Student {
    String name;
    int studentID;
    
    
    Student(String s,int id)
    {
        name=s;
        studentID=id;
    }
    Student()
    {
    
    }
    
   void showDetails()
   {
        System.out.println("Student name is " +name);
         System.out.println("Student ID  is " +studentID);
   
   }
    public static void main(String args[])
    {
    Student s =new Student("Eric Mutua",222221);
    s.showDetails();
    Student s2=new Student();
    s2.name=s.name;
    s2.studentID=s.studentID;
    s2.showDetails();
   
    }
    
}


Related Articles:

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Constructor Overloading in Java
Constructor Overloading in Java
Constructor Overloading in Java
Java Codes
http://www.download-all.net/2017/04/constructor-overloading-in-java.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/04/constructor-overloading-in-java.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy