0
Contructors vs getters-setters
What is the advantage/disadvantage of using constructors over getters/setters? Are they interchangeable? Is one method preferred to the other?
3 Answers
+ 1
Values for the constructors are assigned when the class gets instantiated.
But getters and setters are not
+ 1
Do not get a variable with a constructor.
Constructors are generally used to initialize values upon object creation.
Setters can set values at any time.
Getters can get the variables/values at any time.
You don't use setters and getters instead of constructors, you use constructors for initial values and getters or setters for later use.
+ 1
A constructor is used when the object is created.
You can initialize you properties in the constructor.
During the life time of a object, getters and setters are used to get information about the object and to set the properties of the object.
You need to use both.