Constructor in Java
Constructors
Objects are allocated with the new operator using an object constructor. A constructor is a special method with the same name as its class and no return type. It’s called when a new class instance is created, which gives the class an opportunity to set up the object for use. Constructors, like other methods, can accept arguments and can be overloaded. they are not, however, inherited like other methods.There are two types of constructors:
1. Default constructor (no-arg constructor)
A constructor that has no parameter is known as default constructor. If we don’t define a constructor in a class, then compiler creates default constructor(with no arguments) for the class. And if we write a constructor with arguments or no-argument then compiler does not create default constructor. Default constructor provides the default values to the object like 0, null etc. depending on the type.Syntax
class_name()
{
}
Program
class Car
{
//car constructor
Car()
{
System.out.println("new car is ready.");
}
}
public class Default_Constructer
{
public static void main(String args[])
{
//object creation
Car swift = new Car();
}
}
Output
new car is ready.
Explanation
In this example, the class Car has one constructors. which takes no arguments; it’s known as the default constructor. Default constructors play a special role: if we don’t define any constructors for a class, an empty default constructor is supplied for us. The default constructor is what gets called whenever you create an object by calling its constructor with no arguments. Here we have implemented the default constructor so that it sets the instance swift.If there is no constructor in a class, compiler automatically creates a default constructor.Default constructor provides the default values to the object like 0, null etc. depending on the type.
let see example of default constructor with default value.default constructor with default value
Program
class Car
{
int speed;
String model;
void display()
{
System.out.println(model + " " + speed);
}
}
public class default_value
{
public static void main(String args[])
{
//object creation
Car swift = new Car();
swift.display();
Car eco = new Car();
eco.speed = 500;
//it will display speed but not model
eco.display();
}
}
Output
null 0
null 500
Explanation
In the above class,you are not creating any constructor so compiler provides you a default constructor.Here 0 and null values are provided by default constructor.In second object we have defined speed of that object so, it will display speed but not model.2. Parameterized constructor
A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with your own values, then use parameterized constructor.Syntax
class_name(parameters...)
{
}
Program
class Car
{
int speed;
String model;
//parametarized constructor
Car(int s,String m)
{
speed = s;
model = m;
}
void display()
{
System.out.println(model + " " + speed);
}
}
public class para_constructor
{
public static void main(String args[])
{
//object creation
Car swift = new Car(500,"swift");
swift.display();
}
}
Output
swift 500
Example program with default and parametarized constructor
Program
class Car
{
Car()
{
System.out.println("default constructor");
}
Car(int speed)
{
System.out.println("parametarized constructor");
}
}
public class para_def
{
public static void main (String[] args)
{
Car def = new Car();
Car par = new Car(500);
}
}
Output
default constructor
parametarized constructor
Explanation
In this example, the class Car has two constructors. The first takes no arguments; it’s known as the default constructor. Default constructors play a special role: if we don’t define any constructors for a class, an empty default constructor is supplied for us. The default constructor is what gets called whenever you create an object by calling its constructor with no arguments. The second constructor takes a int argument. Given the constructors in the previous example, we create two Car objects.In each case, Java chooses the appropriate constructor at compile time based on the rules for overloaded method selection.
copy constructor in Java
There is no copy constructor in java. But, we can copy the values of one object to another like copy constructor in C++.There are many ways to copy the values of one object into another in java. They are:
- By constructor
- By assigning the values of one object into another
- By clone() method of Object class
Program
class Car
{
int speed;
String model;
Car(int s,String m)
{
speed = s;
model = m;
}
//copy constructor
Car(Car c)
{
speed = c.speed;
model = c.model;
}
void display()
{
System.out.println(speed + " " + model);
}
}
public class copy_constructor
{
public static void main (String[] args)
{
Car c1 = new Car(500,"eco");
//assigning parameter of c1 to c2
Car c2 = new Car(c1);
c1.display();
c2.display();
}
}
Output
500 eco
500 eco
difference between constructor and method
Java Constructor | Java Method |
---|---|
Constructor is used to initialize the state of an object. | Method is used to expose behaviour of an object. |
Constructor must not have return type. | Method must have return type. |
Constructor is invoked implicitly. | Method is invoked explicitly. |
The java compiler provides a default constructor if you don't have any constructor. | Method is not provided by compiler in any case. |
Constructor name must be same as the class name. | Method name may or may not be same as class name. |
Comments
Post a Comment