Constructors in Java Programming

Constructors in Java Programming

Constructors in Java Programming or other object-oriented languages are special methods that are called automatically whenever an object is created. It initializes class attributes for a particular object and allocates memory to it. If a Java program doesn’t have a constructor, then there is a default constructor present that does nothing.

Syntax of Constructor Creation:

class_name(){
statements;
}

Rules of Constructor Creation:

    • Constructors have the same name as the class they are a part of.
    • They do not have a return type; not even void. They return nothing.
    • They cannot be static, abstract, final, or synchronized.

 

 

Types of Constructors in Java Programming

In Java, there are two types of constructors:

    • Default Constructor
    • Parameterized Constructor

Default Constructors in Java Programming

It does not have any parameters. Its role is to provide default values to the object’s data members like null and zero depending upon their type.

Syntax of Default Constructor:

ClassName(){
}

Example of Default Constructor:

//Java Program with default constructor
public class DefaultConstructor {
    //default constructor
    DefaultConstructor(){
        System.out.println("This is a default constructor.");
    }
    public static void main(String[] args) {
        DefaultConstructor obj=new DefaultConstructor();
    }
}

Output:

This is a default constructor.

 

 

Parameterized Constructor in Java Programming

It is a constructor that takes parameters and initialized object’s data members to some values passed during its creation. It helps to create distinct objects with different values to their members but can have the same values as well.

Syntax of Parameterized Constructor:

ClassName(int a){
this.data_member=a;
}

Example of Parameterized Constructor:

//Java Program with parameterized constructor
public class Student {
    String name;
    int age;

    //Parameterized constructor
    Student(String name, int age){
        this.name=name;
        this.age=age;
    }
    public static void main(String[] args) {
        //object instantiation
        Student s=new Student("Aryan", 21);

        //displaying values of s's data members
        System.out.println(s.name);
        System.out.println(s.age);
        
    }
}

Output:

Aryan
21

 

 

Constructor Overloading in Java Programming

In Java, constructor overloading is the same as method overloading where methods having the same name are overloaded in terms of a number of parameters and types of parameters. They are designed to perform separate initializations depending on the number and type of parameters passed to them during object creation and the call to them is resolved by the interpreter on the basis of number and type of parameters.

Example of Constructor Overloading:

//Java Program with constructor overloading
public class Student {
    String name;
    int age;

    //Default constructor
    Student(){
    }

    //Parameterized constructor with 1 parameter of type String
    Student(String name){
        this.name=name;
    }

    //Parameterized constructor with 1 parameter of type int
    Student(int age){
        this.age=age;
    }

    //Parameterized constructor with 2 parameters
    Student(String name, int age){
        this.name=name;
        this.age=age;
    }
    public static void main(String[] args) {
        //calling parameterized constructor with 2 parameters
        Student s1=new Student("Aryan", 21);

        //displaying values of s1's data members
        System.out.println(s1.name);
        System.out.println(s1.age);
        System.out.println();


        //calling parameterized constructor with 1 parameter of type String
        Student s2=new Student("Nicholas");

        //displaying values of s2's data members
        System.out.println(s2.name);
        System.out.println(s2.age);
        System.out.println();


        //calling parameterized constructor with 1 parameter of type int
        Student s3=new Student(22);

        //displaying values of s3's data members
        System.out.println(s3.name);
        System.out.println(s3.age);
        System.out.println();

        
        //calling parameterized constructor with 1 parameter of type int
        Student s4=new Student();

        //displaying values of s4's data members
        System.out.println(s4.name);
        System.out.println(s4.age);
    }
}

Output:

Aryan
21

Nicholas
0

null
22

null
0

 

 

Copy Constructor in Java Programming

Java Programming does not have a copy constructor, but values of one object can be copied to other in the following ways:

    • by constructor
    • by assigning values of one object to another
    • by clone() method of Object class

Create Copy Constructor using Constructor

//Java Program with copy constructor
public class Student {
    String name;
    int age;

    //Copy constructor
    Student(Student stud){
        this.name=stud.name;
        this.age=stud.age;
    }

    //Parameterized constructor with 2 parameters
    Student(String name, int age){
        this.name=name;
        this.age=age;
    }
    public static void main(String[] args) {
        //calling parameterized constructor with 2 parameters
        Student s1=new Student("Aryan", 21);

        //displaying values of s1's data members
        System.out.println(s1.name);
        System.out.println(s1.age);
        System.out.println();

        //calling copy constructor with s1
        Student s2=new Student(s1);

        //displaying values of s2's data members
        System.out.println(s2.name);
        System.out.println(s2.age);
        System.out.println();

    }
}

Output:

Aryan
21

Aryan
21

 

Create Copy Constructor by assigning values of other Object

//Java Program with copy constructor
public class Student {
    String name;
    int age;

    //Default constructor
    Student(){
    }

    //Parameterized constructor with 2 parameters
    Student(String name, int age){
        this.name=name;
        this.age=age;
    }
    public static void main(String[] args) {
        //calling parameterized constructor with 2 parameters
        Student s1=new Student("Aryan", 21);

        //displaying values of s1's data members
        System.out.println(s1.name);
        System.out.println(s1.age);
        System.out.println();

        //calling default constructor
        Student s2=new Student();
        s2.name=s1.name;
        s2.age=s1.age;

        //displaying values of s2's data members
        System.out.println(s2.name);
        System.out.println(s2.age);
        System.out.println();

    }
}

Output:

Aryan
21   
     
Aryan
21

 

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *