Re: Calling a constructor from another
- From: "H. S. Lahman" <h.lahman@xxxxxxxxxxx>
- Date: Mon, 23 May 2005 15:25:44 GMT
Responding to Sathyaish...
How does a constructor of one class call another of the same class?
A constructor is essentially just a class-level responsibility, so it is just another method call (except there is no implied 'this' argument).
When would this mechanism make sense or be required?
The most obvious need is for inheritance. Since the object being instantiated is a composite of all the properties in all classes in the direct line of ascent in the subclassing tree, the usual technique is to provide a constructor for each class to handle that class' properties and invoke them in a nested fashion.
PS: Two instances I saw are:
(a) While using the Singleton pattern (b) While using the Factory pattern
In both cases only one constructor is being called -- the specific object constructor. The methods being invoked by clients in both cases are just ordinary responsibilities. In Singleton the method is usually implemented as a static class method. In Factory the method is just a routine responsibility of Factory class. As it happens, to implement the method responsibilities they need to invoke constructors.
************* 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
.
- References:
- Calling a constructor from another
- From: Sathyaish
- Calling a constructor from another
- Prev by Date: Re: Types of constructors
- Next by Date: Re: Lahman, how ya doing?
- Previous by thread: Re: Calling a constructor from another
- Next by thread: Re: Types of constructors
- Index(es):
Relevant Pages
|