This question is related to How should an `Employee` class be designed?
In the above question, to uniquely identify an employee, each Employee object has an id field as shown below
class Employee
{
private int id // id is given by the database
// Employee data (let's say, dozens of properties).
//methods.....
}
The id of an employee object is given by the database.
So if I use the object for describing a new employee, there will be no id to store yet. But an object representing an existing employee will have an id. So I have a property that sometimes describes the object and sometimes doesn't.
Also, id = null makes the Employee object's state invalid but
[A] properly encapsulated object cannot be brought into an invalid state via the public interface [...]
https://softwareengineering.stackexchange.com/a/258281/234665
Does this design violate encapsulation because id = null at the start?
id = nullmake the state invalid? The linked question does not say that and the accepted answer seems to imply that it should be considered valid. – Stop harming Monica Apr 04 '19 at 21:13id = nullmakes the state invalid? Anyway the answer to your question is obviously yes, what you describe are not properly encapsulated objects. But you already know that don't you? – Stop harming Monica Apr 05 '19 at 09:54id = nullstate is in valid – Susantha7 Apr 05 '19 at 18:11Employeeare not properly encapsulated. Is that what you want to know? – Stop harming Monica Apr 05 '19 at 19:32Employeeobject it's id value equals tonull, I'm not sure are you telling creating object withid = nullis that not make object state invalid...? – Susantha7 Apr 05 '19 at 19:49