default constructor 300x100 1 - Constructor in Java

Constructor in Java

Posted on

Definition : A Constructor  is used to initialize a newly created object and is called just after the memory is allocated for the object. It can be used to initialize the objects with the default values at the time of object creation. It is not mandatory for the coder to write a constructor for the class. If a coder does not write a constructor then the compiler generates a default constructor.

Each time an object is created using new() keyword at least one constructor is invoked to assign initial values to the data members of the same class.

E.g. :

class  Student

{

     String name;

    int rollno;

   Student(String name, int rollno)

        {

            this.name=name;

            this.rollno=rollno;

        }

public static void main (String[] args)

{

        Student s1=new Student (“nandini”,100);

        Student s2=new Student (“priyanka”,101);

}

}

—————————————————————————————————————————————-

output : nandini 100

                  priyanka 101

—————————————————————————————————————————————

Rules for writing Constructors

  1. Constructor name should match with class name .
  2. Constructor can use any access specifier, they can be declared as private also. Private constructors are possible in java but their scope is within the class only.
  3. Constructors do not have any return type. We can differentiate between methods and constructors with the help of return type because methods have return types.
  4. If you don not implement any constructor within the class, compiler will do it automatically and this constructor is called default constructor. Default constructor is a no-arg constructor as it does not have any argument.
  5. this() and super() should be the first statement in the constructor code. If you don’t mention them, compiler does it for you accordingly.
  6. Constructor overloading is possible but overriding is not possible. Which means we can have overloaded constructor in our class but we can’t override a constructor.
  7. Constructors can not be inherited.
  8. If Super class doesn’t have a no-arg (default) constructor then compiler would not insert a default constructor in child class as it does in normal scenario.
  9. Interfaces do not have constructors.
  10. Abstract class can have constructor and it gets invoked when a class, which implements interface, is instantiated.
  11. A constructor can also invoke another constructor of the same class – By using this(). If you want to invoke a parameterized constructor then do it like this: this(parameter list).

 

Types of constructors :

  • Default constructors
  • No argument constructor
  • Parameterized constructors

 Default constructors :

  • If we don’t define a constructor in a class, then compiler creates default constructor(with no arguments) for the class
  • default constructor is a no-arg constructor.
  • Default constructor provides the default values to the object like 0, null etc. depending on the type.
  • if we write a constructor with arguments or no-argument then compiler does not create default constructor.
  • The access modifier of a default constructor is exactly same as the access of the class.

E.g.

 default constructor 300x100 - Constructor in Java 

Parameterized constructor :

A constructor having arguments or parameters is called parameterized constructor.If we define only parameterized constructors, then we cannot create an object with default constructor. This is because compiler will not create default constructor. You need to create default constructor explicitly.

E.g.

public class ParaCons

{

       String name;

   public ParaCons(String name)

           {

           this.name = name;

          System.out.println(“parameterized constructor.”);

          System.out.println(“The parameter value is: “+str);

              }

 public static void main(String a[]){

 ParaCons p = new  ParaCons(“Raj”);

    }

}

—————————————————————————————————————————————-

Output : parameterized Constructor

                 The parameter value is Raj

—————————————————————————————————————————————-

Constructor overloading :

Within a class we can declare multiple constructors and all these having same name and different arguments hence all these constructors are considered as overloaded constructors. 

E.g.  

public class ConsOverloading {

    public ConsOverloading()

    {

        System.out.println(“Default constructor”);

    }

    public ConsOverloading(int i)

    {

    System.out.println(“Integer parameter constructor”);

      }

    public ConsOverloading(String str)

      {

      System.out.println(“String parameter constructor”);

       }

    public ConsOverloading(int i, int j)

            {

            System.out.println(” Two integer parameter constructor”);

             }

 public static void main(String a[]){

        ConsOverloading c = new ConsOverloading();

        ConsOverloading c1 = new ConsOverloading(10);

        ConsOverloading c2= new ConsOverloading(“string”);

        ConsOverloading c3 = new ConsOverloading(10,20);        

    }

}

—————————————————————————————————————————————-

 Output : Default constructor

                   Integer parameter constructor

                  String parameter constructor

                  Two integer parameter constructor

 —————————————————————————————————————————————