Constructors

A constructor is used to initialise the members of a class. A constructor looks like a method but it does not have a return type. It has the same name as that of the class. Following is the format of a constructor.

public <classname> () {
// code
}

The above constructor doesn’t accept any parameters and is known as a default constructor.

Given below is a program called Book which has two instance members bookName and bookNumber, which are initialised to “none” and -1 using a default constrcutor.

public class Book {

    String bookName;
    int bookNumber;

    public Book() {
        bookName = "none";
        bookNumber = -1;
    }
}

A constructor can also accept parameters. The instance members of a class are initialised using the accepted parameters. Such constructors are called parameterized constructors.

Given below is an example of the same class Book described above having a parameterized constructor.

public class Book {

    String bookName;
    int bookNumber;

    public Book(String name, int number) {
        bookName = name;
        bookNumber = number;
    }
}

A class can also atmost one default constructor and any number of parameterised constructors.

Here is the class Book with one default constructor and one parameterised constructor.

public class Book {

    String bookName;
    int bookNumber;

    public Book() {
        bookName = "none";
        bookNumber = -1;
    }

    public Book(String name, int number) {
        bookName = name;
        bookNumber = number;
    }
}

But, we should be able to differentiate between two parameterised constructors by looking at the parameter lists ( without the actual names ). To check whether we can have two given parameter lists in the same class, write the parameter lists without the variable names and see if they are different. If they are different, then we can have both of them in the same class. Else, it is not allowed.

Let us take some examples :

Example 1 :

Suppose the two parameterized constructors are
public Book ( int number ) …
public Book ( String name ) …
Writing the parameter list without the variable names, we get
( int )
( String )
As both of them are different, we can have them in the same class.
That is, the following class definition is valid.

public class Book {

    String bookName;
    int bookNumber;

    public Book(String name) {
        bookName = name;
    }

    public Book(int number) {
        bookNumber = number;
    }
}

Example 2 :
Now consider the following two constructors :
public Book ( String bookName ) …
public Book ( String authorName ) …
Writing the two parameter lists of the two constructors without the varaible names, we get:
( String )
( String )
As the above two are the same, the two constructors of this example cannot be included in the same class.

Leave a Reply

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


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>