These languages are different, but in both languages,
java.lang.Object) has a
public constuctor. By default, every class inherits from
Object and they call that constructor. Why is it
public and not
protected? What’s the point of constructing the
Object class (apart from using it to lock)?
It’s useful to be able to construct an instance of Object for a couple of reasons.
The first, as you identified, is in using it as a lock. When you are implementing a multi threaded locked system it is better to have a private instance of a lock rather than using the synchronized keyword on a class to prevent others from accidentally locking on your lock object.
The other case where it is handy is to use it as a tombstone value or other marker value in a list. For example in an array of objects, deleting one value might require a propagation of elements ordinarily, but if you replace that element with a known instance of a value (like DELETED=new Object()) then you can keep the structure of the array intact and then process the individual values while doing an identity equality check to compare it against your known tombstone value.