Reputation: 1041
I saw an example that try to explain inheritance in Java. The class Employee, which is the base class, has three instance variables and three constructors. it is as follows:
public class Employee{
private String name;
private int id;
public Employee(){
name = " No Name!";
id = 00100;
}
public Employee(String n, int i){
name = n;
id = i;
}
public Employee (Employee originalObject){
name = originalObject.name;
id = originalObject.id;
}
My question is : What's the point of the third constructor? and how it accepts an argument with the same type, Employee
,of the class that we are still working on ? The program has already an empty constructor and another one that passes String for name
and int
for id
, so why there is an extra one that does no much more than the previous two constructors ?
Upvotes: 3
Views: 88
Reputation: 612954
The third constructor is not necessary, as you say, but it is provided for convenience to any clients of this class.
It is more concise to write
new Employee(employee)
than to pass all the parameters individually.
It may also be that the required fields are private fields and so may be inaccessible to the code creating new Employee
instances. Your class is a good example since it does not have public methods getName()
and getId()
to expose these properties publicly.
Finally, passing another instance of the class makes it clear to the reader that we are making a new class, that is a duplicate or a copy of the original. Being able to express that intent clearly makes maintenance easier.
Upvotes: 1
Reputation: 785128
It is for convenience and it is called "Copy Constructor" where a Employee instance is created by copying properties of an already existing Employee instance.
Upvotes: 1
Reputation: 11257
It is called copy constructor (the article is for C++, but copy constructor is general conception).
Also this question discusses the difference between shallow and deep copy.
Upvotes: 1
Reputation: 114767
It's a copy constructor - the preferred alternative to clone()
when we need to create a copy of an instance.
Upvotes: 0
Reputation: 198093
This is called a copy constructor, and the point of such constructors is that you are now free to modify this new Employee
object without affecting the original, or vice versa.
You are correct that new Employee(other)
is equivalent to new Employee(other.getName(), other.getId())
, but the copy constructor version is much shorter and makes the intent much clearer -- and for more complicated objects that don't expose all of their state, a copy constructor has access to details that another constructor would not. Additionally, if you add fields to Employee
, you don't have to change all the callers of your constructor -- making your code significantly more maintainable.
Upvotes: 1
Reputation: 993085
It looks like the third constructor is intended to make a copy of an existing Employee
object.
You could probably do the same thing with:
new Employee(orig.getName(), orig.getId());
but that presumes that there are "get" methods for every property. It's easier to implement one constructor like that, and then call:
new Employee(orig);
Then, if the properties of Employee
need to change, you would only have to change the copy constructor in one place.
Upvotes: 5