Re: Types of constructors
- From: "H. S. Lahman" <h.lahman@xxxxxxxxxxx>
- Date: Mon, 23 May 2005 15:15:22 GMT
Responding to Sathyaish...
Caveat: any answer here depends on the specific language implementation. Constructors tend to be very language-dependent.
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides:
(1) public constructors; and (2) parameterized constructors
A private constructor is one that is visible only to the class or members of the class. The most common use is when objects are instantiated through a special static Class method. For example, one might need to support a Find(id) capability and one doesn't want to clutter the constructor itself with the infrastructure for managing an ordered list of instances.
A second use occurs when one member of the class needs to instantiate another member of a class in a special manner. [FWIW, I am not a fan of this because the context of instantiation usually involves business rules and policies that are unique to the problem context. Since objects abstract intrinsic responsibilities of underlying problem space entities, it is hard to rationalize why an object should have that sort of knowledge about its own context of use.]
Another use is when a constructor is parameterized and the parameterization causes the instance to be created with different attributes initialized by default. One can have the parameterized constructor dispatch to the right private constructor. [FWIW, I am not a fan of this sort of parameterization because it implies that in some circumstances the object is created with uninitialized attributes. That strikes me as a marvelous opportunity for foot-shooting. IOW, I think all attributes should be initialized by default.]
And I understand that they are not mutually exclusive of one another. The above classification assimilates my knowledge of having used constructors in both the above manners.
Public and private constructors are mutually exclusive but parameterized constructors can be either.
************* There is nothing wrong with me that could not be cured by a capful of Drano.
H. S. Lahman hsl@xxxxxxxxxxxxxxxxx Pathfinder Solutions -- Put MDA to Work http://www.pathfindermda.com blog: http://pathfinderpeople.blogs.com/hslahman (888)OOA-PATH
.
- Prev by Date: Re: UML: Associations between classes and packages
- Next by Date: Re: Calling a constructor from another
- Previous by thread: Calling a constructor from another
- Next by thread: Re: Types of constructors
- Index(es):
Relevant Pages
|