
A constructor is a special method that is used to initialize an object. Every class has a constructor, if we don’t explicitly declare a constructor for any java class the compiler builds a default constructor for that class.
* Rules for create a Java Constructor
- A constructor does not have any return type.
- A constructor has same name as the class in which it resides.
- Constructor in Java cannot be abstract, static, final or synchronized. These modifiers are not allowed for constructor.
Types of java constructors-
There are two types of constructors:
- Default constructor (no-arg constructor)
2. Parameterized constructor
Each time a new object is created at least one constructor will be invoked.
Car c = new Car() //Default constructor invoked
Car c = new Car(name); //Parameterized constructor invoked
Q) What is the purpose of default constructor?
Default constructor provides the default values to the object like 0, null etc. depending on the type.
Constructor Overloading
Constructors in Java may be overloaded. This means that a class can have MORE than one constructor. This is useful for when you want the object to be created with different parameters up front. There are two ways a Java constructor can be overwritten: give the new constructor a different number of parameters, or give the new constructor at least one different type of parameter.
Example of constructor overloading
class Cricketer
{
String name;
String team;
int age;
Cricketer () //default constructor.
{
name =””;
team =””;
age = 0;
}
Cricketer(String n, String t, int a) //constructor overloaded
{
name = n;
team = t;
age = a;
}
Cricketer (Cricketer ckt) //constructor similar to copy constructor of c++
{
name = ckt.name;
team = ckt.team;
age = ckt.age;
}
public String toString()
{
return “this is ” + name + ” of “+team;
}
}
Class test:
{
public static void main (String[] args)
{
Cricketer c1 = new Cricketer();
Cricketer c2 = new Cricketer(“sachin”, “India”, 32);
Cricketer c3 = new Cricketer(c2 );
System.out.println(c2);
System.out.println(c3);
c1.name = “Virat”;
c1.team= “India”;
c1.age = 32;
System .out. print in (c1);
}
}
output:
this is sachin of india
this is sachin of india
this is virat of india
Important points to be remember about Construction :-
- Every class has a constructor whether it’s normal one or a abstract class.
- As stated above, constructor are not methods and they don’t have any return type.
- Constructor name and class name should be the same.
- Constructor can use any access specifier; they can be declared as private also. Private constructors are possible in java but their scope is within the class only.
- Constructor gets called automatically after creation of object and before completion of new Operator
- Like constructor’s method can also have name same as class name, but still they have return type, though which we can identify them that they are methods not constructors.
- If you don’t define any constructor within the class, compiler will do it for you and it will create a constructor for you.
- this() and super() should be the first statement in the constructor code.If you don’t mention them, compiler does it for you accordingly.
- Constructor overloading is possible but overriding is not possible. Which means we can have overloaded constructor in our class but we can’t override a constructor.
- Constructors can not be inherited.
- If Super class doesn’t have a no-arg(default) constructor then compiler would not define a default one in child class as it does in normal scenario.
- Interfaces do not have constructors.
- Abstract can have constructors and these will get invoked when a class, which implements interface, gets instantiated. (i.e. object creation of concrete class).
- A constructor can also invoke another constructor of the same class – By using this(). If you wanna invoke a arg-constructor then give something like: this(parameter list).
Difference between Constructor and Method –
Constructor
|
Method
|
| The purpose of constructor is to create object of a class | The purpose of a method is to perform a task by executing java code. |
| Constructors cannot be abstract, final, static and synchronised | Methods can be abstract, final, static and synchronised |
| Constructors do not have return types | Methods have return types |
| The java compiler provides a default constructor if you don’t have any constructor. | Method is not provided by compiler in any case. |
No comments:
Post a Comment