Objects and Constructors in Java - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Objects and Constructors in Java

Share This

As we have mentioned in the previous tutorial objects are instances of a class. Now, in this chapter take a deeper look into class and objects. Let us start with a very simple example.

If you want to build a house, what will you do as a first step, when you have a prototype (or building plan)?

First, you have to acquire sufficient land. Similarly, when you want to create an object of a class, you have to allocate memory. In Java programming language constructors are used for allocating memory. The house which is built based on the building plan can be considered an object.

If more than one house is built based on the building plan, we can say that multiple objects are created of that class.

A constructor is a code block in a Java class. It is called when an instance of a class is created. If you do not write any constructor, a default constructor is provided with default values of the attributes. But in most of the time, you may need to write some constructors, where initial values are passed as arguments to the constructors.


Difference between Constructor and Method


Note that the constructors and methods are not the same, though they look alike.

  • A constructor does not return any value, whereas a method may return a value.
  • The name of the constructor should be the same as the class name, whereas a method name can be the same as the class name.
  • Constructors are not considered as members of the class.

Let us check whether a method can be named by class name.

ComplexNum.java
public class ComplexNum { float x; float y; // method public void ComplexNum() { // write logic of the function } // constructor public ComplexNum(int x, int y) { this.x = x; this.y = y; } }


Constructor in Detail


Let us start with another example of employee class. The class contains a set of attributes, It does not contain any method or constructors. Hence, if you create an object of this class, the attributes will be set by default values. Though this class does not have any constructor, an instance of the class can be created.

Note that if a class does not have any constructor, a default constructor is provided by the language.

Employee.java
public class Employee { int _empid; String _name; String _designation; String _address; String _gender; String _dob; String _dateOfJoining; public void printValues() { System.out.println("Employee [_empid=" + _empid + ", _name=" + _name + ", _designation=" + _designation + ", _address=" + _address + ", _gender=" + _gender + ", _dob=" + _dob + ", _dateOfJoining=" + _dateOfJoining + "]"); } }
}


TestMain.java
public class TestMain { public static void main(String[] args) { Employee emp = new Employee(); emp.printValues(); } }

A constructor can be created by setting some values to the attributes. For example, if we want to set designation with Trainee, we have to define a constructor. Thus, the above class can be redefined as follow:

Employee.java
public class Employee { int _empid; String _name; String _designation; String _address; String _gender; String _dob; String _dateOfJoining; public Employee() { this._designation = "Trainee"; } public void printValues() { System.out.println("Employee [_empid=" + _empid + ", _name=" + _name + ", _designation=" + _designation + ", _address=" + _address + ", _gender=" + _gender + ", _dob=" + _dob + ", _dateOfJoining=" + _dateOfJoining + "]"); } }


Parameterized Constructor


Thus, if you want to create an object with some values of the attributes (not default values), you have to create a constructor.

Now, look at the following example, where a constructor is created. Here, the parameters of the constructor are passed at the time of creating an instance of the class.

Employee.java
public class Employee { int _empid; String _name; String _designation; String _address; String _gender; String _dob; String _dateOfJoining; // parameterized constructor................... public Employee(int _empid, String _name, String _designation, String _address, String _gender, String _dob, String _dateOfJoining) { super(); this._empid = _empid; this._name = _name; this._designation = _designation; this._address = _address; this._gender = _gender; this._dob = _dob; this._dateOfJoining = _dateOfJoining; } public void printValues() { System.out.println("Employee [_empid=" + _empid + ", _name=" + _name + ", _designation=" + _designation + ", _address=" + _address + ", _gender=" + _gender + ", _dob=" + _dob + ", _dateOfJoining=" + _dateOfJoining + "]"); } }
TestMain.java
public class TestMain { public static void main(String[] args) { Employee emp1 = new Employee(2, "John Smith", "Trainee", "1/A, XYZ Street, ABC", "Male", "10/12/1998", "13/3/2018"); emp1.printValues(); // Employee emp2 = new Employee(); // default constructor not provided } }

Remember that the default constructor is provided if the class does not contain any constructor.




Happy Exploring!

No comments:

Post a Comment