views:

314

answers:

9

I wonder if can I say that a constructor is a special case of a method?

+9  A: 

You can say anything. Whether anyone will disagree with you depends on the context. Some language communities and standards define things that way.

More elaborately, it depends on what you mean by a 'method.' In C++, for example, one way to analyze the creation process is to say that it consists of a call to an operator new (perhaps just placement) followed by a call to a constructor method. From an implementation standpoint, a constructor looks, walks, and quacks like a method. In some compilers, you can even invoke one explicitly.

From a more theoretical viewpoint, someone might claim that constructors are some distinctive species. However, there is no single, true, privileged conceptual model of methods, constructors, or purple unicorns.

Gosh this is all subjective.

bmargulies
"You can say anything." But remember "Anything you say can and will be used against you in a court of law." =)
kemiisto
@kemiisto +1 for your comment, as I don't see a real value of the chosen answer :-) I think the other answers would together render into a pretty good and elaborate answer.
Ondrej Tucny
@Ondrej Tucny: and I don't see a value of the question itself.
kemiisto
@kemiisto If you don't think as a user of a programming language, then you're right and it makes not much sense to ask such a question. However, if you study programming language design, it's quite a fundamental question with many aspects.
Ondrej Tucny
+1 to Ondrej, I agree with you ("don't see a real value of the chosen answer" :)
Gan
@ond if you want to have an intellectual discussion about this, you would need (a) to specify the terms of reference and (b) take it to the new Programmers.Stackexchange.com site.
bmargulies
@bmargulies my comment applied when your answer didn't state much more than “You can say anything.” I don't think the topic needs to be discusses any further.
Ondrej Tucny
A: 

I would say not at all. A constructor instantiates an object whereas a method manipulates an already existing object. To be more clear methods Access and Mutate Constructed Objects.

Woot4Moo
I believe a destructor would be considered a method, yes I agree the uniqueness of the paradigm
Woot4Moo
So then static methods aren't methods because they don't necessarily need an instance of an object?
Timothy
I don't really agree with this. Methods can be declared static (C#) / shared (VB) and they are not tied to existing object, ie. you do not need to instantiate an object to use those methods.
Gan
Timothy the term static means the object exists at compile time as opposed to runtime
Woot4Moo
A static method means that you don't need an instance of its class to call it. No objects exist at compile-time.
Mark Cidade
I was under the impression that the class is loaded into memory regardless of if it is actually instantiated.
Woot4Moo
The class is loaded into memory regardless, but only at runtime.
Mark Cidade
+1  A: 

I think a constructor is too special to be called a method

  • It doesn't return anything
  • It modifies the object before the object is initialized
  • It cannot call itself (imagine that)

blah blah blah

There might be difference between languages, but I don't think I'm going as far as calling a constructor "special method".

phunehehe
you can't modify something before it exists
Woot4Moo
hmm right, "initialized" is a better word
phunehehe
Constructor does not return anything. Methods can also be declared to return nothing (void in C++).
Gan
void is a return type whereas a constructor does not have a return type
Woot4Moo
@Woot4Moo: do you know that there are couple of programming languages (VB.NET, Pascal , Fortran, Ada, ...) which distinguish between "functions" or "function subprograms", which provide an explicit return value to the calling program, and "subroutines" or "procedures", which do not? Subroutines which doesn't return anything is so typical in such languages.
kemiisto
@Woot4Moo: yes, void is a return type. What I was trying to convey is that a constructor is similar to a void method (both do not return anything).
Gan
+9  A: 

Technically, a constructor usually is a method. Whether it really is or is not depends largely on the particular environment. For example, in .NET constructors are methods called actually after an object is created. However, it's also possible to create an object without having a constructor called right after.

Update: Regarding .NET, or the Common Language Infrastructure to be more precise, ECMA 335, section 8.9.6.6 Constructors states:

New values of an object type are created via constructors. Constructors shall be instance methods, defined via a special form of method contract, which defines the method contract as a constructor for a particular object type.

Ondrej Tucny
How can you say it usually is a method when it doesn't have a return type?
Woot4Moo
Providing a method is a named bunch of executable code (in a very simplistic sense), why a method should have a “return type”?
Ondrej Tucny
Technically, it depends on context a lot. E.g., in Java there's clear difference between terms 'constructor' and 'method' (don't know about C#). Also, here http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming)
Nikita Rybak
@Nikita Actually the common meaning of a 'method' is the 'code' in the 'object = data + code' equation :-) In programming language design it's useful to have one single abstraction for a "named buch of executable code", and the 'method' paradigm fit quite well. From that, treating constructors as special methods is quite obvious. Note that all the quotes and apostrophes are quite necessare here in this discussion as most terms are not defined properly and in the context of a specific OO programming language / abstract OOP term-set.
Ondrej Tucny
every method has a return type.
Woot4Moo
@Woot4Moo In what language? I think not-so-absolutist claims are appropriate here. For example, in CLI EMCA 335 in section 8.6.1.5 Method signatures says about “return types” that “A method signatures is composed of … a type signature for the result value, **if one is produced**.”
Ondrej Tucny
once again look at the definition of void
Woot4Moo
@Woor4Moo I'm not arguing void is a type. But you are obviously not getting the point, at least in respect the situation in the .NET world, because in .NET a constructor **is** a method **by definition**. See my updated answer above.
Ondrej Tucny
Thanks for the updates
Woot4Moo
A: 

"Special" is the magic word here. There's absolutely nothing wrong with calling a constructor a special method, but what "special" implies can vary depending on the language.

In most cases, "special" means they can't return values or be called as a method without creating a new object. But there are always exceptions: a prime example is JavaScript, where a constructor is no different from a normal function, it can return its own values and it can be called either as a constructor or as a simple function.

casablanca
+2  A: 

In languages that have constructors, you can usually think of a constructor as a special case of a factory method. (Note: I don't mean the GoF Factory Method Software Design Pattern, I'm just talking about any class method that creates new instances.) Usually, this "special casing" generally takes the form of annoying restrictions (e.g. in Java, you can only call the parent constructor at the beginning of the constructor), which is why even in languages that do have constructors, you often end up using or writing factory methods anyway.

So, if constructors are basically factory methods with restrictions, there is really no need to have them both, and thus many languages simply get rid of constructors. Examples include Objective-C, Ruby, Smalltalk, Self, Newspeak, ECMAScript/JavaScript, Io, Ioke, Seph and many others.

In Ruby, the closest thing to a constructor is the method Class#allocate, which simply allocates an empty object and sets that object's class pointer. Nothing more. Since such an empty object is obviously unusable, it needs to initialized. Per convention, this initialization is performed by #initialize. As a convenience, because it is cumbersome to always have to remember to both allocate and initialize (as any Objective-C developer can probably attest), there is a helper method called Class#new, which looks something like this:

class Class
  def new(*args, &block)
    obj = allocate
    obj.initialize(*args, &block)

    return obj
  end
end

This allows you to replace this:

foo = Foo.allocate
foo.initialize(bar)

With this:

foo = Foo.new(bar)

It is important to note that there is nothing special about any of these methods. Well, with one exception: Class#allocate obviously has to be able to set the class pointer and to allocate memory, which is something that is not possible in Ruby. So, this method has to somehow come from outside the system, which e.g. in MRI means that it is written in C, not Ruby. But that only concerns the implementation. There are no special dispatch rules, no special override rules. It's just a method like any other that can e.g. call super whereever, whenever and how often it wants and can return what it wants.

Jörg W Mittag
A: 

@Tom Brito, personally I would agree with you that a constructor is a special case of method.

Also, see below:

A constructor in a class is a special type of subroutine called at the creation of an object.

... A constructor resembles an instance method, but it differs from a method in that it never has an explicit return-type...

Source: Wikipedia

Also, you may read my comments on others' comment (woot4moo, phunehehe).

Gan
+8  A: 
Mark Cidade
A: 

At least in vb.net, constructors can have a non-standard control flow. If the first statement of a constructor is a call to New (of either the same type or a base type), the sequence of events will be: (1) perform the call; (2) initialize all the fields associated with the type; (3) finish handling the rest of the constructor. No normal method has that sort of control flow. This control flow makes it possible to do things like pass constructor parameters to field initializers of a derived type, if the base type is written to allow such.

supercat