QA's approach 2 Java - Constructor

Constructor:

Constructors have the same name as the class itself. Secondly, they do not specify a return type, not even void.

Purpose and function
Constructors have one purpose in life: to create an instance of a class. This can also be called creating an object,

Limitations of Setters & Getters:

Assume if we have 100 instance variables, we need to have 200 setters & getters totally to instantiate all the 100 variables, i.e., one each setter & getter for one instance variable.
1 variable = 1 setter + 1 getter
Solution:

Setter ->problem can be overcome by Constructor

Getter -> can be overcome by toString method

Properties of a constructor

1. Constructor will be called automatically when the object is created.
2. Constructor name must be similar to name of the class.
3. Constructor should not return any value even void also. Because basic aim is to place the value in the object. (if we write the return type for the constructor then that constructor will be treated as ordinary method).
4. Constructor definitions should not be static. Because constructors will be called each and every time, whenever an object is creating.
5. Constructor should not be private provided an object of one class is created in another class (Constructor can be private provided an object of one class created in the same class).
6. Constructors will not be inherited from one class to another class (Because every class constructor is create for initializing its own data members).
7. The access specifier of the constructor may or may not be private.
8. If the access specifier of the constructor is private then an object of corresponding class can be created in the context of the same class but not in the context of some other classes.
9. If the access specifier of the constructor is not private then an object of corresponding class can be created both in the same class context and in other class context.
10. constructors cannot be abstract, final, native, static, or synchronized.


Example:

public class ConstructorExm {

int rollNo;
String name;
double fee;

public ConstructorExm(int rollNo, String name, double fee) {
this.rollNo = rollNo;
this.name = name;
this.fee = fee;
}

public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}

public int getRollNo() {
return rollNo;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public double getFee() {
return fee;
}

public void setFee(double fee) {
this.fee = fee;

}

public String toString() {
return "Student information is as follows" + '\n' + "[rollNo=" + rollNo + '\n' + " name=" + name + '\n'
+ " fee=" + fee + "]";
}

public static void main(String[] args) {
ConstructorExm conObj = new ConstructorExm(100, "AJAY", 10000.00);

System.out.println(conObj.toString());

}

}

Types of constructors

Based on creating objects in Java constructor are classified in two types. They are

1. Default or no argument Constructor
2. Parameterized constructor.

Purpose of default constructor?

Default constructor provides the default values to the object like 0, null etc. depending on the type.

Example:

class Student {
int roll;
float marks;
String name;

void show() {
System.out.println("Roll: " + roll);
System.out.println("Marks: " + marks);
System.out.println("Name: " + name);
}
}

class TestDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.show();
}

}

Why use parameterized constructor?

Parameterized constructor is used to provide different values to the distinct objects.
Needs to be declared explicitly

Example:

class Test {
int a, b;

Test(int n1, int n2) {
System.out.println("I am from Parameterized Constructor...");
a = n1;
b = n2;
System.out.println("Value of a = " + a);
System.out.println("Value of b = " + b);
}
};

class TestDemo1 {
public static void main(String k[]) {
Test t1 = new Test(10, 20);
}

}

Usage of 'this' keyword with constructor

1. Constructors use 'this' to refer to another constructor in the same class with a different parameter list.
2. If a constructor uses this keyword, it must be the 1st statement in constructor.

Usage of 'super' keyword with constructor

1.Constructors use super to invoke the superclass's constructor.
2. If a constructor uses super, it must use it in the first line.

Does constructor return any value?

yes, that is current class instance (You cannot use return type yet it returns a value).

Can constructor perform other tasks instead of initialization?

Yes, like object creation, starting a thread, calling method etc. You can perform any operation in the constructor as you perform in the method. if we want to perform some action immediately on object creation, that's done by passing the method call in the default constructor.

Why overriding is not possible at constructor level?

The scope of constructor is within the class so that it is not possible to achieved overriding at constructor level.

Constructor overloading:

class ClassName {
ClassName()
{
..........
..........
}

ClassName(datatype1 value1)
{.......}

ClassName(datatype1 value1, datatype2 value2)
{.......}

ClassName(datatype2 variable2)
{.......}

ClassName(datatype2 value2, datatype1 value1)
{.......}........
}


Disadvantages of Constructor:

1. constructor is called everytime an object is created


Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?