Why constructor
is not considered as member
of a class ?
Is there any specific reason ?
Thanks and regards.
Why constructor
is not considered as member
of a class ?
Is there any specific reason ?
Thanks and regards.
Members are inherited to subclasses. Constructors must not be inherited, so they are not considered to be members.
Constructors are not inherited, because their task is to initialize attributes of their specific class. Any subclass must initialize its additional attributes, and for this task it needs an own constructor that knows about the additional attributes.
Also, each constructor must call one of its superclass constructors directly or indirectly as its first action, to give the superclass a change for initialization.
"Understanding constructors" : http://www.javaworld.com/jw-10-2000/jw-1013-constructors.html
In C++, constructors certainly are considered to be members of a class - the C++ Standard describes them in a section headed "Special Member Functions".
As i see it,constructor is not a method but a life-cycle hook provided by Java (another life-cycle hook is finalize). This gives you a chance to do things which would be pre-requisite before any method could be called on the object.So there is clear distinction between methods and constructor, hence even the Language specs has it but the intent is not totally clear from the specs.
I reject the premise of the question. A constructor is a member of a class or struct in C#.
I refer you to section 3.4.4 ("Class members") of the C# specification, which enumerates the members of a class:
A class declaration may contain declarations of constants, fields, methods, properties, events, indexers, operators, instance constructors, destructors, static constructors and types.
Clearly constructors are members of a class. Why do you believe that a constructor is not a member? Who told you that lie?
As Eric points out, in C# constructors are considered class members. In fact, it is simply a class method with additional metadata used by the compiler and runtime so the constructor method is invoked during object creation.
Although it doesn't say why, according to the Java Language Specification, Second Edition constructors, static initializers, and instance initializers are not members.