Constructor (object-oriented programming)
This article needs additional citations for verification. (August 2010) |
In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called to create an object. It prepares the new object for use, often accepting parameters that the constructor uses to set member variables required for the object to reach a valid state. It is called a constructor because it constructs the values of data members of the class.
A constructor resembles an instance method, but it differs from a method in that it has no explicit return type, it is not implicitly inherited and it usually has different rules for scope modifiers. Constructors often have the same name as the declaring class. They have the task of initializing the object's data members and of establishing the invariant of the class, failing if the invariant is invalid. A properly written constructor leaves the resulting object in a valid state. Immutable objects must be initialized in a constructor.
Programmers also use the term constructor to denote one of the tags that wraps data in an algebraic data type. This is a different usage than in this article.[dubious – discuss]
Most languages allow overloading the constructor in that there can be more than one constructor for a class, with differing parameters. Some languages take consideration of some special types of constructors.
Types
Parameterized constructors
Constructors that can take arguments are termed as parameterized constructors. The number of arguments can be greater or equal to one(1). For example:
class example
{
int p, q;
public:
example(int a, int b); //parameterized constructor
};
example :: example(int a, int b)
{
p = a;
q = b;
}
When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly.The method of calling the constructor implicitly is also called the shorthand method.
example e = example(0, 50); //explicit call
example e(0, 50); //implicit call
Default constructors
If the programmer does not supply a constructor for an instantiable class, a typical compiler will provide a default constructor. The behavior of the default constructor is language dependent. It may initialize data members to zero or other same values, or it may do nothing at all.
Copy constructors
Copy constructors define the actions performed by the compiler when copying class objects. A copy constructor has one formal parameter that is the type of the class (the parameter may be a reference to an object).
It is used to create a copy of an existing object of the same class. Even though both classes are the same, it counts as a conversion constructor.
A constructor is a special type of method.
Conversion constructors
Conversion constructors provide a means for a compiler to implicitly create an object belonging to one class based on an object of a different type. These constructors are usually invoked implicitly to convert arguments or operands to an appropriate type, but they may also be called explicitly.
Syntax
- Java, C++, C#, ActionScript, and PHP 4 have a naming convention in which constructors have the same name as the class of which they are associated with.
- In PHP 5, a recommended name for a constructor is
__construct
. For backwards compatibility, a method with the same name as the class will be called if__construct
method can not be found. Since PHP 5.3.3, this works only for non-namespaced classes.[1] - In Perl, constructors are, by convention, named "new" and have to do a fair amount of object creation.
- In Moose object system for Perl, constructors (named new) are automatically created and are extended by specifying a BUILD method.
- In Visual Basic .NET, the constructor is called "
New
". - In Python, the constructor is called "
__init__
" and is always passed its parent class as an argument, the name for which is generally defined as "self
". - Object Pascal constructors are signified by the keyword "
constructor
" and can have user-defined names (but are mostly called "Create
"). - In Objective-C, the constructor method is split across two methods, "
alloc
" and "init
" with thealloc
method setting aside (allocating) memory for an instance of the class, and theinit
method handling the bulk of initializing the instance. A call to the method "new
" invokes both thealloc
and theinit
methods, for the class instance.
Memory organization
In Java, C# and VB .NET the constructor creates objects in a special memory structure called heap for reference types. Value types (such as int, double etc.), are created in a sequential structure called stack. VB NET and C# allow use of new to create objects of value types. However, in those languages even use of new for value types creates objects only on stack.
In C++ when constructor is invoked without new the objects are created on stack. When objects are created using new they are created on heap. They must be deleted implicitly by a destructor or explicitly by a call to operator delete.
Default constructor
Most languages provide a default constructor if the programmer does not. However, this default constructor is overridden by an explicit constructor in the class code. In C++ a default constructor is REQUIRED if an array of class objects is to be created. Other languages (Java, C#, VB .NET) have no such restriction.
Language details
Java
In Java, constructors differ from other methods in that:
- Constructors never have an explicit return type.
- Constructors cannot be directly invoked (the keyword “
new
” invokes them). - Constructors cannot be synchronized, final, abstract, native, or static.
Java constructors perform the following tasks in the following order:
- Initialize the class variables to default values. (Byte, short, int, long, float, and double variables default to their respective zero values, booleans to false, chars to the null character ('\u0000') and object references to null.)
- Call the default constructor of the superclass if no constructor is defined.
- Initialize member variables to the specified values.
- Executes the body of the constructor.
Java provides access to the superclass's constructor through the super
keyword.
public class Example
{
//definition of the constructor.
public Example()
{
this(1);
}
//overloading a constructor
public Example(int input)
{
data = input; //This is an assignment
}
//declaration of instance variable(s).
private int data;
}
//code somewhere else
//instantiating an object with the above constructor
Example e = new Example(42);
Visual Basic .NET
In Visual Basic .NET, constructors use a method declaration with the name "New
".
Class Foobar
Private strData As String
' Constructor
Public Sub New(ByVal someParam As String)
strData = someParam
End Sub
End Class
' code somewhere else
' instantiating an object with the above constructor
Dim foo As New Foobar(".NET")
C#
Example C# constructor:
public class MyClass
{
private int a;
private string b;
//constructor
public MyClass() : this(42, "string")
{
}
//overloading a constructor
public MyClass(int a, string b)
{
this.a = a;
this.b = b;
}
}
//code somewhere
//instantiating an object with the constructor above
MyClass c = new MyClass(42, "string");
C# static constructor
In C#, a static constructor is a static data initializer. Static constructors allow complex static variable initialization.[2] Static constructors are called implicitly when the class is first accessed. Any call to a class (static or constructor call), triggers the static constructor execution. Static constructors are thread safe and implement a singleton pattern. When used in a generic programming class, static constructors are called at every new generic instantiation one per type. Static variables are instantiated as well.
public class MyClass
{
private static int _A;
//normal constructor
static MyClass()
{
_A = 32;
}
//standard default constructor
public MyClass()
{
}
}
//code somewhere
//instantiating an object with the constructor above
//right before the instantiation
//the variable static constructor is executed and _A is 32
MyClass c = new MyClass();
C++
In C++, the name of the constructor is the name of the class. It returns nothing. It can have parameters like any member functions (methods). Constructor functions are declared in the public section.
The constructor has two parts. First is the initializer list which follows the parameter list and before the method body. It starts with a colon and entries are comma-separated. The initializer list is not required, but offers the opportunity to provide values for data members and avoid separate assignment statements. The initializer list is required if you have const or reference type data members, or members that do not have parameterless constructor logic. Assignments occur according to the order of the initializer list. The second part is the body, which is a normal method body enclosed in curly brackets.
C++ allows more than one constructor. The other constructors can have different default values for the parameters. The constructor of a base class (or base classes) can also be called by a derived class. Constructor functions are not inherited and their addresses cannot be referenced. When memory allocation is required, the new and delete operators are called implicitly.
A copy constructor has a parameter of the same type passed as const reference, for example Vector(const Vector& rhs). If it is not provided explicitly, the compiler uses the copy constructor for each member variable or simply copies values in case of primitive types. The default implementation is not efficient if the class has dynamically allocated members (or handles to other resources), because it can lead to double calls to delete (or double release of resources) upon destruction.
class Foobar {
public:
Foobar(double r = 1.0, double alpha = 0.0) // Constructor, parameters with default values.
: x(r*cos(alpha)) // <- Initializer list
{
y = r*sin(alpha); // <- Normal assignment
}
// Other member functions
private:
double x; // Data members, they should be private
double y;
};
Example invocations:
Foobar a,
b(3),
c(5, M_PI/4);
Private data member functions appear at the top section before writing the public specifier. If you no longer have access to a constructor then you can use the destructor.[clarification needed]
In C++ the copy constructor is called implicitly when class objects are returned from a method by return mechanism or when class objects are passed by value to a function. C++ provides a copy constructor if the programmer does not. The default copy constructor ONLY makes member-wise copy or shallow copies. For deep copies an explicit copy constructor that makes deep copies is required. For a class to make deep copies, the three methods below must be provided.
- Copy constructor
- Overloading of assignment operator.
- A destructor.
The above is called rule of three in C++. If cloning of objects is not desired in C++ then copy constructor must be declared private.
Failure
A constructor that create a valid value should throw an exception. This is because exceptions should be thrown when post-conditions cannot be met, and the post-condition of a constructor is the existence of a valid object. An object which throws during its constructor never comes into existence (although some of its member objects might). This affects how one handles errors and special consideration must be given for exceptions emitted by member variables' constructors.[3]
F#
In F#, a constructor can include any let
or do
statements defined in a class. let
statements define private fields and do
statements execute code. Additional constructors can be defined using the new
keyword.
type MyClass(_a : int, _b : string) = class
// primary constructor
let a = _a
let b = _b
do printfn "a = %i, b = %s" a b
// additional constructors
new(_a : int) = MyClass(_a, "") then
printfn "Integer parameter given"
new(_b : string) = MyClass(0, _b) then
printfn "String parameter given"
new() = MyClass(0, "") then
printfn "No parameter given"
end
//code somewhere
//instantiating an object with the primary constructor
let c1 = new MyClass(42, "string")
//instantiating an object with additional constructors
let c2 = new MyClass(42)
let c3 = new MyClass("string")
let c4 = MyClass() // "new" keyword is optional
Eiffel
In Eiffel, the routines which initialize new objects are called creation procedures. Creation procedures have the following traits:
- Creation procedures have no explicit return type (by definition of procedure).[4]
- Creation procedures are named.
- Creation procedures are designated by name as creation procedures in the text of the class.
- Creation procedures can be explicitly invoked to re-initialize existing objects.
- Every effective (i.e., concrete or non-abstract) class must designate at least one creation procedure.
- Creation procedures must leave the newly initialized object in a state that satisfies the class invariant.[5]
Although object creation involves some subtleties,[6] the creation of an attribute with a typical declaration x: T
as expressed in a creation instruction create x.make
consists of the following sequence of steps:
- Create a new direct instance of type
T
.[7] - Execute the creation procedure
make
to the newly created instance. - Attach the newly initialized object to the entity
x
.
In the first snippet below, class POINT
is defined. The procedure make
is coded after the keyword feature
.
The keyword create
introduces a list of procedures which can be used to initialize instances. In this case the list includes default_create
, a procedure with an empty implementation inherited from class ANY
, and the make
procedure coded within the class.
class
POINT
create
default_create, make
feature
make (a_x_value: REAL; a_y_value: REAL)
do
x := a_x_value
y := a_y_value
end
x: REAL
-- X coordinate
y: REAL
-- Y coordinate
...
In the second snippet, a class which is a client to POINT
has a declarations my_point_1
and my_point_2
of type POINT
.
In procedural code, my_point_1
is created as the origin (0.0, 0.0). Because no creation procedure is specified, the procedure default_create
inherited from class ANY
is used. This line could have been coded create my_point_1.default_create
.
Only procedures named as creation procedures can be used in an instruction with the create
keyword.
Next is a creation instruction for my_point_2
, providing initial values for the my_point_2
's coordinates.
The third instruction makes an ordinary instance call to the make
procedure to reinitialize the instance attached to my_point_2
with different values.
my_point_1: POINT
my_point_2: POINT
...
create my_point_1
create my_point_2.make (3.0, 4.0)
my_point_2.make (5.0, 8.0)
...
ColdFusion
ColdFusion has no constructor method. Developers using it commonly create an 'init
' method that acts as a pseudo-constructor.
<cfcomponent displayname="Cheese">
<!--- properties --->
<cfset variables.cheeseName = "" />
<!--- pseudo-constructor --->
<cffunction name="init" returntype="Cheese">
<cfargument name="cheeseName" type="string" required="true" />
<cfset variables.cheeseName = arguments.cheeseName />
<cfreturn this />
</cffunction>
</cfcomponent>
Object Pascal
In Object Pascal, the constructor is similar to a factory method. The only syntactic difference to regular methods is the keyword constructor
in front of the name (instead of procedure
or function
). It can have any name, though the convention is to have Create
as prefix, such as in CreateWithFormatting
. Creating an instance of a class works like calling a static method of a class: TPerson.Create('Peter')
.
program OopProgram;
interface
type
TPerson = class
private
FName: string;
public
property Name: string read FName;
constructor Create(AName: string);
end;
implementation
constructor TPerson.Create(AName: string);
begin
FName := AName;
end;
var
Person: TPerson;
begin
Person := TPerson.Create('Peter'); // allocates an instance of TPerson and then calls TPerson.Create with the parameter AName = 'Peter'
end;
Perl
In Perl version 5, by default, constructors must create and return the object, that is create and return a blessed reference. A typical object is a reference to a hash, though rarely references to other types are used too. By convention the only constructor is named new, though it is allowed to name it otherwise, or to have multiple contructors. For example, a Person class may have a constructor named new as well as a constructor new_from_file which reads a file for Person attributes, and new_from_person which uses another Person object as a template.
package Person;
use strict;
use warnings;
# In Perl constructors are named 'new' by convention.
sub new {
# Class name is implicitly passed in as 0th argument.
my $class = shift;
# Default attribute values, if you have any.
my %defaults = ( foo => "bar" );
# Initialize attributes as a combination of default values and arguments passed.
my $self = { %defaults, @_ };
# Check for required arguments, class invariant, etc.
if ( not defined $self->{first_name} ) {
die "Mandatory attribute missing in Person->new(): first_name";
}
if ( not defined $self->{last_name} ) {
die "Mandatory attribute missing in Person->new(): last_name";
}
if ( defined $self->{age} and $self->{age} < 18 ) {
die "Invalid attribute value in Person->new(): age < 18";
}
# Perl makes an object belong to a class by 'bless'.
bless $self, $class;
return $self;
}
1;
Perl with Moose
With the Moose object system for Perl, most of this boilerplate can be left out, a default new is created, attributes can be specified, as well as whether they can be set, reset, or are required. In addition, any extra constructor functionality can be included in a BUILD method which the Moose generated constructor will call, after it has checked the arguments. A BUILDARGS method can be specified to handle constructor arguments not in hashref / key => value form.
package Person;
# enable Moose-style object construction
use Moose;
# first name ( a string) can only be set at construction time ('ro')
has first_name => (is => 'ro', isa => 'Str', required => 1);
# last name ( a string) can only be set at construction time ('ro')
has last_name => (is => 'ro', isa => 'Str', required => 1);
# age (Integer) can be modified after construction ('rw'), and is not required
# to be passed to be constructor. Also creates a 'has_age' method which returns
# true if age has been set
has age => (is => 'rw', isa => 'Int', predicate => 'has_age');
# Check custom requirements
sub BUILD {
my $self = shift;
if ($self->has_age && $self->age < 18) { # no under 18s
die "No under-18 Persons";
}
}
1;
In both cases the Person class is instiated like this:
use Person;
my $p = Person->new( first_name => 'Sam', last_name => 'Ashe', age => 42 );
PHP
In PHP version 5 and above, the constructor is a method named __construct()
, which the keyword new
automatically calls after creating the object. It is usually used to automatically perform initializations such as property initializations. Constructors can also accept arguments, in which case, when the new
statement is written, you also need to send the constructor arguments for the parameters.[1]
class Person
{
private $name;
public function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
Python
In Python, constructors are created by defining a __new__
method, and are called when a new instance is created by calling the class. Unlike other languages such as C++, derived classes in Python do not call their base classes' constructors. However, when a constructor is not defined, the next one found in the class's Method Resolution Order will be called. Due to Python's use of duck typing, class members are often defined in the constructor, rather than in the class definition itself.
In the typical case, however, a class acts as a factory of new instances, thus there is no overriding of the __new__
method: in this case, the __init__
method receives the arguments passed, and initializes the new instance accordingly[8].
class ExampleClass(object):
def __new__(self):
# We override the constructor to return none instead.
return None
exampleInstance = ExampleClass()
print exampleInstance
None
Ruby
In Ruby, constructors are created by defining a method called initialize
. This method is executed to initialize each new instance.
class ExampleClass
def initialize
return "Hello there"
end
end
exampleInstance = ExampleClass.new
puts exampleInstance # => "Hello there"
See also
References
- ^ a b Constructors and Destructors, from PHP online documentation
- ^ Static Constructor in C# on MSDN
- ^ "GotW #66: Constructor Failures". Gotw.ca. 2000-02-02. Retrieved 2013-01-19.
- ^ Eiffel routines are either procedures or functions. Procedures never have a return type. Functions always have a return type.
- ^ Because the inherited class invariant must be satisfied, there is no mandatory call to the parents' constructors.
- ^ Eiffel ISO/ECMA specification document
- ^ The Eiffel standard requires fields to be initialized on first access, so it is not necessary to perform default field initialization during object creation.
- ^ Data model Classes are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that override __new__(). The arguments of the call are passed to __new__() and, in the typical case, to __init__() to initialize the new instance.