Constructor (object-oriented programming): Difference between revisions

Content deleted Content added
 
(26 intermediate revisions by 13 users not shown)
Line 4:
In [[class-based programming|class-based]], [[object-oriented programming]], a '''constructor''' (abbreviation: '''ctor''') is a special type of [[Function (computer programming)|function]] called to [[object creation|create an object]]. It prepares the new object for use, often accepting [[Parameter (computer programming)|arguments]] that the constructor uses to set required [[member variable]]s.
 
A constructor resembles an [[methodMethod (computer scienceprogramming)|instance method]], but it differs from a method in that it has no explicit [[return type]], it is not implicitly [[inheritance (object-oriented programming)|inherited]] and it usually has different rules for scope modifiers. Constructors often have the same name as the declaring [[class (computer science)|class]]. They have the task of [[initialization (computing)|initializing]] the object's [[data member]]s and of establishing the [[Class invariant|invariant of the class]], failing if the invariant is invalid. A properly written constructor leaves the resulting [[object (computer science)|object]] in a ''valid'' state. [[Immutable object]]s must be initialized in a constructor.
 
Most languages allow [[method overloading|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. Constructors, which concretely use a single class to create objects and return a new instance of the class, are abstracted by [[Factory (object-oriented programming)|factories]], which also create objects but can do so in various ways, using multiple classes or different allocation schemes such as an [[object pool]].
Line 13:
 
<syntaxhighlight lang="cpp">
class ExamplePoint {
private:
public:
Example() int x;
int y;
Example(int a, int b); // Parameterized constructor.
public:
 
Point() = default;
private:
Point(int x, int x_;y):
x{x}, y{y} {} // Parameterized constructor
int y_;
};
 
Example::Example() = default;
 
Example::Example(int x, int y) : x_(x), y_(y) {}
</syntaxhighlight>
 
<syntaxhighlight lang="cpp">
ExamplePoint ep = ExamplePoint(0, 50); // Explicit call.
ExamplePoint e2p2(0, 50); // Implicit call.
</syntaxhighlight>
 
=== Default constructors ===
If the programmer does not supply a constructor for an instantiable class, Java compiler inserts a [[default constructor]] into your code on your behalf. This constructor is known as default constructor. You would not find it in your source code (the java file) as it would be inserted into the code during compilation and exists in .class file. 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. In Java, a "default constructor" refer to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class or in the absence of any programmer-defined constructors (e.g. in Java, the default constructor implicitly calls the [[Superclass (computer science)|superclass]]'s [[nullary]] constructor, then executes an empty body). All fields are left at their initial value of 0 (integer types), 0.0 (floating-point types), false (boolean type), or null (reference types)...
 
<syntaxhighlight lang="cpp">
class Point {
#include <iostream>
private:
 
int x;
class Student {
int y;
public:
public:
Student(int a = 0, int b = 0); // Default constructor.
Point(int x = 0, int y = 0); // Default constructor.
 
int a;
int b;
};
</syntaxhighlight>
Line 50 ⟶ 44:
=== Copy constructors ===
{{see also|Copy constructor (C++)}}
Like C++, Java also supports "Copy ConstructorConstructors". But, unlike C++, Java doesn't create a default copy constructor if you don't write your own. 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.
While copy constructors are usually abbreviated copy ctor or cctor, they have nothing to do with class constructors used in [[.NET]] using the same abbreviation.
 
Line 60 ⟶ 54:
 
== Syntax ==
* [[Java (programming language)|Java]], [[C++]], [[C Sharp (programming language)|C#]], [[ActionScript]], {{nowrap|[[PHP]] 4}}, and [[MATLAB]] have a naming convention in which constructors have the same name as the class with which they are associated.
* In [[Rust (programming language)|Rust]], the convention for the "constructor" is to name it <code>new</code>.
* In PHP 5, a recommended name for a constructor is <code>__construct</code>. For backwards compatibility, a method with the same name as the class will be called if <code>__construct</code> method can not be found. Since PHP 5.3.3, this works only for non-namespaced classes.<ref name="php5cpnstructor">[http://www.php.net/manual/en/language.oop5.decon.php Constructors and Destructors], from PHP online documentation</ref>
* In PHP 7, you should always name the constructor as <code>__construct</code>. Methods with the same name as the class will trigger an E_DEPRECATED level error.<ref name="php5cpnstructor">[http://www.php.net/manual/en/language.oop5.decon.php Constructors and Destructors], from PHP online documentation</ref>
* In [[Perl]], constructors are, by convention, named "new" and have to do a fair amount of object creation.
* In [[Moose perl|Moose object system]] for Perl, constructors (named ''new'') are automatically created and are extended by specifying a ''BUILD'' method.
Line 71 ⟶ 66:
 
== Memory organization ==
In Java, C#, and VB .NET, the constructor creates reference type objects on the heap, whereas primitive types (such as <code>int</code>, <code>double</code>, etc.) are stored on the [[Stack-based memory allocation|stack]] (though some languages allow for manually allocating objects on the stack through a <code>stackalloc</code> modifier). VB .NET and C# also allow the use of the <code>new</code> operator to create value type objects, but these value type objects are created on the stack regardless of whether the operator is used or not. In these languages, object destruction occurs when the object has no references and then gets destroyed by the garbage collector.
In Java, C#, and VB .NET, the constructor creates reference type objects in a special memory structure called the
"[[heap (data structure)|heap]]". Value types (such as int, double, etc.) are created in a sequential structure called the "[[stack (abstract data type)|stack]]".
VB .NET and C# also allow the use of the ''new'' operator to create value type objects, but these value type objects are created on the stack regardless of whether the operator is used or not.
 
In C++, objects are created on the stack when the constructor is invoked without the <code>new</code> operator, and created on the heap when the constructor is invoked with the <code>new</code> operator (which returns a pointer to the object). Stack objects are deleted implicitly when they go out of scope, while heap objects must be deleted implicitly by a destructor or explicitly by using the ''<code>delete''</code> operator. By using the "[[Resource Acquisition is Initialization]]" (RAII) idiom, resource management can be greatly simplified.
 
== Language details ==<!-- see also Category:Programming language comparisons -->
Line 83 ⟶ 76:
In [[C++]], the name of the constructor is the name of the class. It returns nothing. It can have parameters like any [[Method (computer programming)|member function]]. Constructor functions are usually declared in the public section, but can also be declared in the protected and private sections, if the user wants to restrict access to them.
 
The constructor has two parts. First is the [[initializer list]] which follows the [[parameter (computer science)|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 in which data members are declared (even if the order in the initializer list is different).<ref>https://stackoverflow.com/questions/1242830/constructor-initialization-list-evaluation-order Constructor</ref> The second part is the body, which is a normal method body enclosed in curly brackets. It is generally cheaper and better practice to use the initializer list as much as possible, and only use the constructor body for non-assignment operations and assignments where the initializer list cannot be used or is otherwise insufficient.
 
C++ allows more than one constructor. The other constructors must have different parameters. Additionally constructors which contain parameters which are given default values, must adhere to the restriction that not all parameters are given a default value. This is a situation which only matters if there is a default constructor. 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.
Line 90 ⟶ 83:
 
<syntaxhighlight lang="cpp">
import std;
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
}
 
class PolarPoint {
private:
private:
double x_;
double y_x;
double y;
public:
PolarPoint(double r = 1.0, double theta = 0.0): // Constructor, parameters with default values.
x{r * std::cos(theta)}, y{r * std::sin(theta)} /* <- Initializer list */ {
std::println("Point: x = {}, y = {}", x, y); // Constructor body
}
};
</syntaxhighlight>
Example invocations:
<syntaxhighlight lang="cpp">
PolarPoint a;
Foobar a,
PolarPoint b(3),;
PolarPoint c(5, M_PIstd::numbers::pi / 4);
</syntaxhighlight>
 
Line 145 ⟶ 137:
 
==== C# static constructor ====
In [[C Sharp (programming language)|C#]], a ''static constructor'' is a static data initializer.<ref name="Albahari">{{cite book |last=Albahari |first=Joseph |title= C# 10 in a Nutshell |publisher= O'Reilly |isbn= 978-1-098-12195-2}}</ref>{{rp|111-112}} Static constructors are also called ''class constructors''. Since the actual method generated has the name ''.cctor'' they are often also called "cctors".<ref>{{cite web|url=http://ericlippert.com/2013/02/06/static-constructors-part-one/ |title=Fabulous Adventures in Coding |publisher=Eric Lippert |date=2013-02-06|access-date=2014-04-05}}</ref><ref>{{cite book|url=https://books.google.com/books?id=oAcCRKd6EZgC&pg=PA222 |title=Expert .NET 2.0 IL Assembler |publisher=APress |date=2006-01-01|isbn=9781430202233 |access-date=2014-04-05}}</ref>
 
Static constructors allow complex static variable initialization.<ref>[http{{Cite web|url=https://msdnwww.microsoft.com/en-us/librarydownload/k9x6w0hc%28VS.80%29details.aspx?id=55984|title=Download StaticVisual ConstructorStudio in2005 C#Retired documentation from Official Microsoft Download Center|website=Microsoft Store - onDownload MSDN]Center}}</ref>
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.<ref name=Skeet>{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}</ref>{{rp|38}}<ref name="Albahari>{{cite book |last=Albahari |first=Joseph |title= C# 10 in a Nutshell |publisher= O'Reilly |isbn= 978-1-098-12195-2}}<"/ref>{{rp|111}} Static variables are instantiated as well.
 
<syntaxhighlight lang="csharp">
Line 182 ⟶ 174:
 
'''Cheese.cfc'''
<syntaxhighlight lang="javascriptcfc">
component {
// properties
Line 196 ⟶ 188:
 
Create instance of a cheese.
<syntaxhighlight lang="javascriptcfc">
myCheese = new Cheese( 'Cheddar' );
</syntaxhighlight>
Line 202 ⟶ 194:
Since ColdFusion 10,<ref>[https://wikidocs.adobe.com/wiki/display/coldfusionen/cfcomponent CFComponent]</ref> CFML has also supported specifying the name of the constructor method:
 
<syntaxhighlight lang="javascriptcfc">
component initmethod="Cheese" {
// properties
Line 225 ⟶ 217:
* Creation procedures must leave the newly initialized object in a state that satisfies the class invariant.{{Efn|Because the inherited class invariant must be satisfied, there is no mandatory call to the parents' constructors.}}
 
Although object creation involves some subtleties,<ref name="eiffel standard">[{{Cite web|url=http://www.ecma-international.org/publications/standards/Ecma-367.htm |title=Eiffel ISO/ECMA specification document]}}</ref> the creation of an attribute with a typical declaration <code lang="eiffel">x: T</code> as expressed in a creation instruction <code lang="eiffel">create x.make</code> consists of the following sequence of steps:
 
* Create a new direct instance of type <code lang="eiffel">T</code>.{{Efn|The Eiffel standard requires fields to be initialized on first access, so it is not necessary to perform default field initialization during object creation.}}
Line 322 ⟶ 314:
 
Java permit users to call one constructor in another constructor using <code>this()</code> keyword.
But <code>this()</code> must be first statement. <ref>[{{Cite web|url=https://ranjeetkumarmaurya.wordpress.com/2017/02/06/constructor-in-java/ |title=Details on Constructor in java]}}</ref>
 
<syntaxhighlight lang="java">
class ExampleX {
public X() { // Non-parameterized constructor
{
Example() // Non-parameterized constructor
{
this(1); // Calling of constructor
System.out.println("0-arg-consCalling default constructor");
}
 
Example(int a) // Parameterized constructor
public X(int a) { // Parameterized constructor
{
System.out.println("1-arg-consCalling parameterized constructor");
}
}
 
public static void main(String[] args)
public class Example {
{
public static void main(String[] args) {
Example e = new Example();
X x = new X();
}
}
</syntaxhighlight>
Line 346 ⟶ 338:
 
<syntaxhighlight lang="java">
public class ExampleX {
// Declaration of instance variable(s).
{
private int data;
 
// Definition of the constructor.
public ExampleX() {
{
this(1);
}
 
// Overloading a constructor
public ExampleX(int input) {
{
data = input; // This is an assignment
}
}
 
class Y extends X {
// Declaration of instance variable(s).
private int datadata2;
 
public Y() {
super();
data2 = 1;
}
 
public Y(int input1, int input2) {
super(input1);
data2 = input2
}
}
</syntaxhighlight>
 
public class Example {
<syntaxhighlight lang="java">
public static void main(String[] args) {
// Code somewhere else
Y y = new Y(42, 43);
// Instantiating an object with the above constructor
}
Example e = new Example(42);
}
</syntaxhighlight>
 
A constructor taking zero number of arguments is called a "no-arguments" or "no-arg" constructor.<ref>{{cite web|url=http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html|title= Providing Constructors for Your Classes |publisher=Oracle Corporation|date=2013|access-date=2013-12-20}}</ref>
 
=== JavaScript/TypeScript ===
As of ES6, [[JavaScript]] has direct constructors like many other programming languages. They are written as such
 
<syntaxhighlight lang="javascript">
class FooBar {
constructor(baz) {
this.baz = baz;
}
}
</syntaxhighlight>
Line 387 ⟶ 390:
 
<syntaxhighlight lang="javascript">
const foo = new FooBar('7');
</syntaxhighlight>
 
Line 394 ⟶ 397:
<syntaxhighlight lang="javascript">
function FooBar (baz) {
this.baz = baz;
}
</syntaxhighlight>
 
This is instantiated the same way as above.
 
The [[TypeScript]] equivalent of this would be:
<syntaxhighlight lang="typescript">
class FooBar {
baz: string;
 
constructor(baz: string) {
this.baz = baz;
}
}
 
const foo: FooBar = new FooBar('7');
</syntaxhighlight>
 
=== Object Pascal ===
Line 428 ⟶ 444:
 
=== OCaml ===
In [[OCaml]], there is one constructor. Parameters are defined right after the class name. They can be used to initialize instance variables and are accessible throughout the class. An anonymous hidden method called <code>initializer</code> allows to evaluate an expression immediately after the object has been built.<ref>[http{{Cite web|url=https://camlocaml.inria.frorg/pubmanual/docs5.3/manualindex.html|title=OCaml -ocaml/ The OCaml manual]Manual|website=ocaml.org}}</ref>
 
<syntaxhighlight lang="ocaml">
Line 536 ⟶ 552:
 
=== Python ===
In [[Python (programming language)|Python]], constructors are defined by one or both of <code>__new__</code> and <code>__init__</code> methods. A new instance is created by calling the class as if it were a function, which calls the <code>__new__</code> and <code>__init__</code> methods. If a constructor method is not defined in the class, the next one found in the class's [[C3 linearization|Method Resolution Order]] will be called.<ref name="auto">[{{Cite web|url=https://docs.python.org/3/reference/datamodel.html|title=3. Data model]|website=Python documentation}}</ref>
 
In the typical case, only the <code>__init__</code> method need be defined. (The most common exception is for immutable objects.)
 
<syntaxhighlight lang="pyconpython">
>>> class ExampleClass:
... def __new__(cls: type, value: int) -> 'ExampleClass':
... print("Creating new instance...")
... # Call the superclass constructor to create the instance.
... instance: 'ExampleClass' = super(ExampleClass, cls).__new__(cls)
... return instance
 
... def __init__(self, value):
def __init__(self, value: int) -> None:
... print("Initialising instance...")
print("Initialising instance...")
... self.payload = value
self.payload: int = value
>>> exampleInstance = ExampleClass(42)
 
if __name__ == "__main__":
exampleInstance: ExampleClass = ExampleClass(42)
print(exampleInstance.payload)
</syntaxhighlight>
 
This prints:
<pre>
Creating new instance...
Initialising instance...
>>> print(exampleInstance.payload)
42
</pre>
</syntaxhighlight>
 
Classes normally act as [[Factory (object-oriented programming)|factories]] for new instances of themselves, that is, a class is a callable object (like a function), with the call being the constructor, and calling the class returns an instance of that class. However the <code>__new__</code> method is permitted to return something other than an instance of the class for specialised purposes. In that case, the <code>__init__</code> is not invoked.<ref>[https://docs.python.org/3/reference/datamodel.html#object.__new__ Data model]<name="auto"/ref>
 
=== Raku ===
Line 623 ⟶ 646:
Hello there
=> #<ExampleClass:0x007fb3f4299118>
</syntaxhighlight>
 
=== Rust ===
[[Rust (programming language)|Rust]] does not have constructors in the sense of object-oriented programming, but often structs have a <code>new()</code> method that essentially acts as a constructor. The return type is usually indicated as <code>Self</code>.
 
<syntaxhighlight lang="rust">
struct Point {
x: i32,
y: i32,
}
 
impl Point {
pub fn new(x: i32, y: i32) -> Self {
Point { x, y }
}
}
 
fn main() {
let p: Point = Point::new(10, 20);
println!("Point is at ({}, {})", p.x, p.y);
}
</syntaxhighlight>