Pages

Constructors

Constructors
Definition

A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations except that they use the name of the class and have no return type.


Rules for Java Constructor
  1. A Constructor must have no explicit return type.
  2. A Constructor should be support Overloading Concepts.
  3. A Constructor name must be the same as its class name.
  4. A Constructor should not be support Overridding Concepts.
  5. A Constructor cannot be abstract, static, final, and synchronized keywords.

Types of Constructor

Default Constructor : If we do not create any constructor, the Java compiler automatically create a no arguments constructor during the execution of the program. This constructor is called default constructor.

package com.jaladhi.jst;
public class Default {
      int id;
      boolean b;
      public static void main(String [] args) {
            Default dc = new Default();
            System.out.println("Example For Default Constructor.");
            System.out.println("Primitive Integer Value : " + dc.a);
            System.out.println("Primitive Boolean Value : " + dc.b);
      }
}
Output :
Primitive Integer Value : 0
Primitive Boolean Value : false.

No Argument Constructor : If a constructor does not accept any parameters, it is known as a No Argument Constructor. A Constructor may or may not have any parameters that means arguments.

package com.jaladhi.jst;
public class NoArgument {
      int id;
      String name;
      public NoArgument(String id, String name) {
            id = 1000;
            name = "Jaladhi Soft Technology";
      }
      public static void main(String [] args) {
            NoArgument nag = new NoArgument();
            System.out.println("ID : " + nag.id);
            System.out.println("Name : " + nag.name);
      }
}
Output :
ID : 1000
Name : Jaladhi Soft Technology.

Parameterized Constructor : A Java constructor can also accept one or more parameters such constructors are known as parameterized constructors with parameters.

package com.jaladhi.jst;
public class Parameterized {
      int id;
      String name;
      public Parameterized(String id, String name) {
            this.id = id;
            this.name = name;
      }
      public static void main(String [] args) {
            Overloading para = new Overloading(1000, "Jaladhi Soft Technology");
            System.out.println("ID : " + para.id);
            System.out.println("Name : " + para.name);
      }
}
Output :
ID : 1000
Name : Jaladhi Soft Technology.

Constructors Overloading in Java

If we can also create two or more constructors with different parameters in Class. This is called Constructors Overloading.

package com.jaladhi.jst;
public class Overloading {
      int id;
      String name;
      String city;
      public Overloading(int id) {
            this.id = id;
      }
      public Overloading(String name, String city) {
            this.name = name;
            this.city = city;
      }
      public static void main(String [] args) {
            Overloading over = new Overloading(2000);
            Overloading over = new Overloading("Jaladhi Soft Technology", "Delhi");
            System.out.println("ID : " + over.id);
            System.out.println("Name : " + over.name);
            System.out.println("City : " + over.city);
      }
}
Output :
ID : 2000
Name : Jaladhi Soft Technology.
City : Delhi

Constructor as Private in Java

Once a constructor is declared private, it cannot be accessed from outside the class. So, creating objects from outside the class is prohibited using the private constructor. Here, we are creating the object inside the same class and the program is able to access the constructor.

package com.jaladhi.jst;
public class PrivateConstructor {
      String name;
      private PrivateConstructor() {
            name = "Jaladhi Soft Technology";
            System.out.println("Called Private Constructor.");
      }
      public static void main(String [] args) {
            PrivateConstructor over = new PrivateConstructor();
            System.out.println("Name : " + over.name);
      }
}
Output :
Called Private Constructor
Jaladhi Soft Technology
Call One Constructor from another Constructor
package com.jaladhi.jst;
public class JST {
      public String fullName;
      public JST() {
            this("Jaladhi", "Soft Technology");
      }
      public JST(String firstName, String lastName) {
            this.fullName = firstName + lastName;
      }
      public void display() {
            System.out.println("Full Name : " + fullName);
      }
      public static void main(String [] args) {
            JST jst = new JST();
            jst.display();
      }
}
Output : Jaladhi Soft Technology.

Call the Constructor of the Super Class from the Constructor of the Child Class
package com.jaladhi.jst;
public class VoteForAge {
      public VoteForAge(int min, int max) {
            if(min < max) {
                  System.out.println("Your are eligible for Vote : " + max);
            } else {
                  System.out.println("Your are not eligible for Vote : " + min);
            }
      }
}
package com.jaladhi.jst;
public class JSTMain extends VoteForAge {
      public JSTMain() {
            super(23, 40);
      }
      public static void main(String [] args) {
            JSTMain jst = new JSTMain();
      }
}
Output : Your are eligible for Vote : 40

Different Between Constructor and Method
Constructor
  1. A Constructor’s name must be same as the name of the class.
  2. A Constructor can be used to initialize an object.
  3. A Constructor is invoked implicitly by the system.
  4. A Constructor doesn’t have a return type.
  5. A Constructor initializes a object that does not exist.
  6. A Constructor cannot be inherited by subclasses.
  7. A Constructor is invoked when a object is created using the keyword new.
  8. A class can have many Constructors but must not have the same parameters.
  9. A Constructor is a block of code that initializes a newly created object.
Method
  1. A Method’s name can be anything.
  2. A Method consists of Java code to be executed.
  3. A Method is invoked by the programmer.
  4. A Method must have a return type.
  5. A Method does operations on an already created object.
  6. A Method can be inherited by subclasses.
  7. A Method is invoked through method calls.
  8. A class can have many methods but must not have the same parameters.
  9. A Method is a collection of statements which returns a value upon its execution.