2 Types of Constructors in Java | Example | Constructor Overloading

2 Types of Constructors in Java | Example | Constructor Overloading

As a part of the Java tutorial series, in the previous tutorial, we learned about classed and objects in Java.

This short tutorial will help you understand the basics about constructor and, types of constructors in Java. I also explain to you – What are the basics rules for writing constructor in Java? And what is constructor overloading in Java? Explained with an example.

So let’s begin.

What is Constructor in Java?

A constructor is a block of code which gets called when an instance of the object is defined and a portion of memory is assigned to it. In simple words, a constructor is a method that is employed to initialize an object that is made with the help of a new keyword.

Make a note of the fact that it is not a compulsion to write a constructor for a particular class as the compiler creates a constructor by default for at runtime.

The basic rule of creating constructor is that its name must be the same as that of the class name with no return type.

Types Of Constructors in Java:

Java defines 2 varied types of constructors, namely:

You can also mix these two constructors for a single class called as…

Default Constructor:

A no-argument constructor is referred to as a default constructor. Such constructors are defined to assign default values to the variable used by the class such as null, 0, 0.0 etc with respect to their data type.

A simple example of default constructor is as follows:

public class Radio
{
    Radio()
    {             
        System.out.println(“A constructor of Radio is invoked”);
    }

    public static void main(String args [])
    {
        Radio r = new Radio();
    }
}

Save and execute your Java program.

Please refer – compiling and running program in Java, if you are new to Java programming.

Output:

A constructor of Radio is invoked.

Here in the above-given example, a class Radio is created with a default constructor that gets called over the creation of the object.

Parameterized Constructor:

A constructor with a set of definite arguments is referred to as a parameterized constructor. Such constructors are used to offering distinct values to varied objects of any particular class.

A simple example of the parameterized constructor is:

public class School
{
    String name;
    String contact;

    School(String n, String c)
    {
        name = n;
        contact = c;
    }

    void printData()
    {
        System.out.println(“Name:”+name+”\tContact:”+contact);
    }

    public static void main(String args[])
    {
       School s1 = new School(“Oxford High School”,”9638527410”);

       School s2 = new School(“Cambridge International School”,”9175346097”);

       School s3 = new School(“Nath Valley Central School”,”8529637410”);

       s1.printData();

       s2.printData();

       s3.printData();
    }
}

Output:

Oxford High School         9638527410
Cambridge International School                                9175346097
Nath Valley Central School           8529637410

What is Constructor Overloading in Java?

Can we write multiple constructors in Java?

Yes, you can. [This question was asked in many job coding and interviews rounds.]

You can use multiple constructors in single calss. The types and number of parameters should not be the same for any of the constructor. When you create a class object, constructor gets called with matching (same types and a number of paratmenrts) constructor function.

It is also called Constructor Overloading in Java programming.

Basic Rules for writing Constructor in Java:

  • It is not necessary to write a constructor in your program.
  • By default, the compiler creates constructor at runtime.
  • The name of the constructor should be the same as the name of the class.
  • You can write multiple constructors for a single class object in Java.

So you understood the main difference between types of constructors in Java. If you have any point to discuss, kindly write a comment section below.



Leave a Reply

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