Misplaced Pages

Constructor (object-oriented programming): Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 18:16, 6 June 2006 edit68.59.61.191 (talk) grammar?← Previous edit Revision as of 02:42, 9 June 2006 edit undoTardis (talk | contribs)Extended confirmed users2,167 edits rewrite pseudocode section; copyeditNext edit →
Line 1: Line 1:
In ], a '''constructor''' in a class is a special ] (function) that can be used to create objects of the class and never has a return type. Constructors are special ] that are called automatically upon the creation of an ] (instance of a class). They are often distinguished by having the same name as the ] of the object they're associated with. Its main purpose is to pre-define the object's ] and to establish the ] of the class, failing if the invariant isn't valid. A properly written constructor will leave the ] in a 'valid' state. In ], a '''constructor''' in a class is a special ] (function) that can be used to create objects of the class and never has a return type. Constructors are special ] that are called automatically upon the creation of an ] (instance of a class). They are often distinguished by having the same name as the ] of the object they're associated with. Its main purpose is to pre-define the object's ] and to establish the ] of the class, failing if the invariant isn't valid. A properly written constructor will leave the ] in a 'valid' state.


<math>Insert formula here</math>=== ] === === ] ===
==== Example ==== ==== Example ====
public class Example public class Example
Line 31: Line 31:
End Class End Class


==Constructors Simplified== ==Constructors Simplified (with ])==


Constructors exist within classes. Classes in programming language refer to a generic specfication which are not real, but just a prototype. A simple analogy to understanding constructors would be to imagine the following scenerios: Compare class in this case to a class of students. Thus we have Constructors are always part of the implementation of classes. A class (in programming) refers to a specfication of the general characteristics of the set of objects that are members of the class rather than the specific characteristics of any object at all. A simple analogy follows. Consider the set (or class, using its generic meaning) of students at some school. Thus we have


<pre>class Student {
===]===
<pre>class Students{
// refers to the class of students // refers to the class of students
// ... more omitted ...
}</pre> }</pre>


However, the class Students is just a generic prototype of what a student should be. Let's analyse this from the position of the IS manager should the school wants to create a database of every student in a computerised database. Let's assume in this case that they want to create each student as an object. Thus they apply the code below to create a prototype or "mould" to create an instance of the class Students (ie to create a real student under the class Students). These moulds are often used as constructors. However, the class <code>Student</code> is just a generic prototype of what a student should be. To use it, the programmer creates each student as an ''object'' or ''instance'' of the class. This object is a real quantity of data in memory whose size, layout, characteristics, and (to some extent) behavior are determined by the class definition. The usual way of creating objects is to call a constructor (classes may in general have many independent constructors). For example,


<pre>class Student {
===]===
Student (String studentName, String sex, String Address, int ID) {

// ... storage of input data and other internal fields here ...
<pre>class Students{
}

// ... more omitted ...
Students (String studentName, char sex, String Address, int ID) {
} }


// elsewhere:
} </pre>
Student john = Student("John Doe", "male", "under the sea", 42);
</pre>


It is up to the class to decide what to do with the information provided (storing it within the new object is very common, but it might be stored in an entirely different format, passed to other constructors or functions and then forgotten, or even disregarded altogether). The constructed object is persistent, and (depending on the class) may be augmented, examined, or modified in ways related or not to the constructor:
Once done, these specific students that we create using the above are also known as instances of the class Students. To create these instances, we use constructors within a class. A constructor is the same as a mould defining the specifications of the student mould we describe above. It defines the type of student, with variables that distinguishes it from other instances. Constructors are normally named the same as their class name. Thus, to create a constructor of a student in a class with some predefined variables, we do the following:


<pre>
Every student will have characteristics that distinguishes them. For example, students have their individual Name, Sex, Address, Unique ID etc. We want to create a prototype, or "mould" for these students such that we can construct each student's profile using simple code like Students("John","M","Dover Road", 12345). If the analyst wants to print out the information for its students, it can use the following code;
print(john.studentName); // output "John Doe", the internal data
print(john.getFirstName()); // object calculates just "John", which is printed
john.addNickName("Johnny"); // object remembers this
</pre>


Here it is assumed that the class provides the <code>studentName</code> variable and the <code>getFirstName()</code> and <code>addNickName()</code> functions, and that the variable is simply assigned the value of the first argument to the constructor. Note that this code does not explicitly mention the class <code>Student</code>; it is known that the variable <code>john</code> is an instance of the class, so the elements of the variable are understood to be members of the same class. This implicit specification is a powerful mnemonic, although it is not fundamentally more powerful than ].
===]===
<pre> print(Students.studentName) // output "John"
</pre>


==See also== ==See also==

Revision as of 02:42, 9 June 2006

In object-oriented programming, a constructor in a class is a special method (function) that can be used to create objects of the class and never has a return type. Constructors are special instance methods that are called automatically upon the creation of an object (instance of a class). They are often distinguished by having the same name as the class of the object they're associated with. Its main purpose is to pre-define the object's data members and to establish the invariant of the class, failing if the invariant isn't valid. A properly written constructor will leave the object in a 'valid' state.

Java

Example

public class Example 
{ 
  //declaration of instance variable(s).
  protected int data;
  //definition of the constructor. 
  public Example()
  {
     data = 1;
     data = 2;
  }
}

REALbasic

Example

Constructors in REALbasic can be in one of two forms. Each form uses a regular method declaration with a special name (and no return value). The older form uses the same name as the Class itself, and the newer form uses the name "Constructor." The newer form is the preferred one because it makes refactoring your class easier.

Class Foobar
  // Old form
  Sub Foobar( someParam as String )
  End Sub
  // New form
  Sub Constructor()
  End Sub
End Class

Constructors Simplified (with pseudocode)

Constructors are always part of the implementation of classes. A class (in programming) refers to a specfication of the general characteristics of the set of objects that are members of the class rather than the specific characteristics of any object at all. A simple analogy follows. Consider the set (or class, using its generic meaning) of students at some school. Thus we have

class Student {
	// refers to the class of students
	// ... more omitted ...
}

However, the class Student is just a generic prototype of what a student should be. To use it, the programmer creates each student as an object or instance of the class. This object is a real quantity of data in memory whose size, layout, characteristics, and (to some extent) behavior are determined by the class definition. The usual way of creating objects is to call a constructor (classes may in general have many independent constructors). For example,

class Student {
	Student (String studentName, String sex, String Address, int ID) {
		// ... storage of input data and other internal fields here ...
	}
	// ... more omitted ...
}
// elsewhere:
Student john = Student("John Doe", "male", "under the sea", 42); 

It is up to the class to decide what to do with the information provided (storing it within the new object is very common, but it might be stored in an entirely different format, passed to other constructors or functions and then forgotten, or even disregarded altogether). The constructed object is persistent, and (depending on the class) may be augmented, examined, or modified in ways related or not to the constructor:

	print(john.studentName); // output "John Doe", the internal data
	print(john.getFirstName()); // object calculates just "John", which is printed
	john.addNickName("Johnny"); // object remembers this

Here it is assumed that the class provides the studentName variable and the getFirstName() and addNickName() functions, and that the variable is simply assigned the value of the first argument to the constructor. Note that this code does not explicitly mention the class Student; it is known that the variable john is an instance of the class, so the elements of the variable are understood to be members of the same class. This implicit specification is a powerful mnemonic, although it is not fundamentally more powerful than function overloading.

See also

References

Category: