Java – Constructors

A function Object() is a particular method of a class that is called whenever a new object of the class is created. It enables the class to allocate memory and populate object properties. Let us know more before we move to Java – Constructors.

The function Object()  function is stated similarly to a standard method, with the exception that the class and method names must be identical and there must be no return type.

When a function Object()  is not given in a class, the compiler constructs and inserts a default function Object() into the code. It does not, however, populate object properties.

Syntax

//Creating class constructor
className(parameters) {
  statements;
}

Example:

A class named Circle is created in the example below. A function Object()  is also constructed, which is used to set the object’s radius attribute.

public class Circle {
  //class attribute
  int radius;
  //class constructor
  public Circle(int x) {
    radius = x;
  }
  //class method
  public double area() {
    return 22/7.0*radius*radius;
  }

  public static void main(String[] args) {
    Circle Cir1 = new Circle(5);
    Circle Cir2 = new Circle(10);

    System.out.println(Cir1.area());
    System.out.println(Cir2.area());
  }
}


The output of the above code will be:

78.57142857142857
314.2857142857143

Constructor Overloading

Constructors, like methods, can be overloaded in Java. Two or more constructors can be built in the same class with various definitions, such as the varied numbers and kinds of parameters, thanks to Java’s function Object()  overloading functionality. The function Object()  that matches the argument supplied while constructing the object is automatically called by the compiler.

Example:

Two constructors are built in this case. To initialise an object of the class Circle, the first function Object() accepts no parameters and the second function Object() takes one parameter.

Cir1 and Cir2 are two Circle objects produced in the main function.

Cir1 initialises the object using the default function Object() . Whereas Cir2 initialises the object with the function Object() that accepts one parameter.

public class Circle {
  //class attribute
  int radius;
  //class constructor without parameter
  public Circle() {
    radius = 1;
  }  
  //class constructor with one parameter
  public Circle(int x) {
    radius = x;
  }
  //class method
  public double area() {
    return 22/7.0*radius*radius;
  }

  public static void main(String[] args) {
    Circle Cir1 = new Circle();
    Circle Cir2 = new Circle(10);

    System.out.println(Cir1.area());
    System.out.println(Cir2.area());
  }
}


The output of the above code will be:

3.142857142857143
314.2857142857143

 

If a function Object() accepts no inputs to initialise the object, it is termed a default function Object().

 

To open the online compiler click here.

Also Read: Java – Encapsulation

Leave a Reply

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