Advertisement

Constructor Method in Java I Constructor overloading

WHAT IS A CONSTRUCTOR

    "It is a special type of method same name as class name that determines how an object is initialized when it's created".


what is constructor in java


 Like other methods, we can also define constructor Method in our java program but unlike other methods, we cannot call a constructor directly; Java called constructor automatically when an object has created. When we use new keyword to create an object of a class, java does three thing;

    1. Allocates memory for the object.
    2. Initialize that object instance variable, with their initial value or to a default.
    3. Call the constructor Method of the class.

If a class doesn't have defined any constructor method, we will still create an object of that class but we have to set instance variable or call other methods that object needs to initialize itself to that object afterward. 

By defining constructor method in our own classes, we can set initial values of instance variable, call method based on those variable or call methods on other objects, or calculate initial properties of our object. We can also overload constructor, as we would regular methods, to create an object that has specific properties based on the argument we give to new.


BASIC CONSTRUCTOR


by defining a constructor looks like a regular method, with 2 basic difference.

    1. Constructor and class name are always same.
    2. It doesn't have any return type

For example, in the below table a simple class person, with a constructor that initializes it's instance variable based on the argument to new. The class also includes a method for the object to introduce itself, and a main() method to test each of these class.


 

class Person
{
    String name;
    int age;

    Person (String n, int a)
{
    name = n;
    age = a;
}

void printPerson ()
{
System.out.print("Hi, I am " +name);
System.out.println(" I am "+ age + " years old.");
}

public static void main(String args[])
{

    Person p;

    p = new Person ("Ajab", 20);
    p.printPerson();

    p = new Person ("Rizwan", 30);
    p.printPerson();


The output of the  program is given below:

Hi, I am Ajab. I am 20 years old.
Hi, I am Rizwan. I am 30 years old



CONSTRUCTOR OVERLOADING


    like other methods, constructor can also take different number and types of parameters, enabling us to create our objects with exactly the properties you want it to have, or for it to be able to calculate properties from different kinds of input.

constructor overloading in Java


For example, the MyRectone class in the given table creates a MyRectone Constructor and passing different parameter instead of creating different methods for the given arguments.

 

class MyRectone
 {

    int x1 = 0;
    int y1 = 0;
    int x2 = 0;
    int y2 = 0;

MyRectone ( int x1, int x2, int x2, int y2)
 {

    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;

}

MyRectone (Point topLeft, Point bottomRight)
 {

    x1 = topLeft.x;
    y1 = topLeft.y;
    x2 = bottomRight.x;
    y2 = bottomRight.y;

}

MyRectone ( Point topLeft, int w, int h)
{

    x1 = topLeft.x;
    y1 = top left.y;
    x2 = ( x1 + w);
    y2 = (y1 + h);

}

void printRect ()
{

    System.out.print ("MyRectone: <" + x1 + ", " + y1);
    system.out.println (", " + x2 + " ,"+ y2 + ">");

}

public static void main (String args [] )
{

    MyRectone rect;
    System.out.println ("Calling MyRectone with coordinates 35,35 70,70");

    rect = new MyRectone (35,35,70,70);
    rect.printRect();

    System.out.println ("Calling MyRectone with coordinates (15,15) (30,30)");
    rect = new MyRectone (15,15,30,30);
    rect.printRect();

    System.out.print (" Calling buildRect w/1 point (10,10),");
    System.out.println ("width (50) and height (50)");
    rect = new MyRectone ( new Point (10,10), 50, 50);
    rect.printRect();


Output

    Calling MyRectone with coordinates 35,35 70,70:
    MyRectone: <35,35,70,70>

    Calling buildRect w/1 points (15,15), (30,30):
    MyRectone: <15,15,30,30>

    Calling buildRect w/1 point (10,10), width (50) and height (50):
    MyRectone:<10,10,50,50>



CALLING ANOTHER CONSTRUCTOR

    Some constructor may be a superset of another constructor defined in your class; that is, they might have the same behavior plus a little bit more. Rather than duplicating identical behavior in multiple constructor Methods in our class, it makes sense to be able to just call that first constructor from inside the body of the second constructor. Java provides a special syntax for doing this. To call a constructor defined on the current class, use this form:

this (arg1, arg2, arg3 …..);

The arguments to this are, of course, the arguments to the constructor.



Post a Comment

0 Comments