Friday 6 December 2013

Constructors in Object Oriented Programming

Constructors may be a new concept for structured programmers. Although constructors are not
normally used in non-OO languages such as COBOL, C, and Basic, the struct , which is part of
C/C++, does include constructors.In some OO languages, such as Java and C#, constructors are
methods that share the same name as the class. Visual Basic .NET uses the designation New
and Objective-C uses the init keyword.
 For example, a constructor for the Cabbie class  would look like this:
 public Cabbie(){
 /* code to construct the object */
 }
 The compiler will recognize that the method name is identical to the class name and consider
the method a constructor.
 Note that in this Java code (as with C# and C++), a constructor does not have a return value. If
you provide a return value, the compiler will not treat the method as a constructor.
 For example, if you include the following code in the class, the compiler will not consider this
a constructor because it has a return value—in this case, an integer:
 public int Cabbie(){
 /* code to construct the object */
 }
 This syntax requirement can cause problems because this code will compile but will not behave
as expected.


Constructors may be a new concept for structured programmers. Although constructors are not 
normally used in non-OO languages such as COBOL, C, and Basic, the struct , which is part of 
C/C++, does include constructors. In the first two chapters, we alluded to these special methods 
that are used to construct objects. In some OO languages, such as Java and C#, constructors are 
methods that share the same name as the class. Visual Basic .NET uses the designation New 
and Objective-C uses the init keyword. As usual, we will focus on the concepts of constructors 
and not cover the specific syntax of all the languages. Let’s take a look at some Java code that 
implements a constructor. 
 For example, a constructor for the Cabbie class we covered in Chapter 2 would look like this: 
 public Cabbie(){
 /* code to construct the object */
 } 
 The compiler will recognize that the method name is identical to the class name and consider 
the method a constructor. 54 Chapter 3 Advanced Object-Oriented Concepts
 Caution 
 Note that in this Java code (as with C# and C++), a constructor does not have a return value. If 
you provide a return value, the compiler will not treat the method as a constructor. 
 For example, if you include the following code in the class, the compiler will not consider this 
a constructor because it has a return value—in this case, an integer: 
 public int Cabbie(){
 /* code to construct the object */
 } 
 This syntax requirement can cause problems because this code will compile but will not behave 
as expected. 
Constructors may be a new concept for structured programmers. Although constructors are not 
normally used in non-OO languages such as COBOL, C, and Basic, the struct , which is part of 
C/C++, does include constructors. In the first two chapters, we alluded to these special methods 
that are used to construct objects. In some OO languages, such as Java and C#, constructors are 
methods that share the same name as the class. Visual Basic .NET uses the designation New 
and Objective-C uses the init keyword. As usual, we will focus on the concepts of constructors 
and not cover the specific syntax of all the languages. Let’s take a look at some Java code that 
implements a constructor. 
 For example, a constructor for the Cabbie class we covered in Chapter 2 would look like this: 
 public Cabbie(){
 /* code to construct the object */
 } 
 The compiler will recognize that the method name is identical to the class name and consider 
the method a constructor. 54 Chapter 3 Advanced Object-Oriented Concepts
 Caution 
 Note that in this Java code (as with C# and C++), a constructor does not have a return value. If 
you provide a return value, the compiler will not treat the method as a constructor. 
 For example, if you include the following code in the class, the compiler will not consider this 
a constructor because it has a return value—in this case, an integer: 
 public int Cabbie(){
 /* code to construct the object */
 } 
 This syntax requirement can cause problems because this code will compile but will not behave 
as expected. 
When Is a Constructor Called? 
 When a new object is created, one of the first things that happens is that the constructor is 
called. Check out the following code: 

 Cabbie myCabbie = new Cabbie(); 

 The new keyword creates a new instance of the Cabbie class, thus allocating the required 
memory. Then the constructor itself is called, passing the arguments in the parameter list. The 
constructor provides the developer the opportunity to attend to the appropriate initialization. 
 Thus, the code new Cabbie() will instantiate a Cabbie object and call the Cabbie method, 
which is the constructor. 

What’s Inside a Constructor? 
Perhaps the most important function of a constructor is to initialize the memory allocated 
when the new keyword is encountered. In short, code included inside a constructor should set 
the newly created object to its initial, stable, safe state. 
 For example, if you have a counter object with an attribute called count , you need to set count 
to zero in the constructor: 
 count = 0; 


The Default Constructor 
 If you write a class and do not include a constructor, the class will still compile, and you can 
still use it. If the class provides no explicit constructor, a default constructor will be provided. 
It is important to understand that at least one constructor always exists, regardless of whether 
you write a constructor yourself. If you do not provide a constructor, the system will provide a 
default constructor for you. 
 Besides the creation of the object itself, the only action that a default constructor takes is to 
call the constructor of its superclass. In many cases, the superclass will be part of the language 
framework, like the Object class in Java. For example, if a constructor is not provided for the 
 
public Cabbie(){
 super();
 } 
If you were to decompile the bytecode produced by the compiler, you would see this code. The 
compiler actually inserts it. 

Using Multiple Constructors
public class Count {
 
 int count;
public Count()
{
 count = 0;
 }
 } 
 On the one hand, we want to initialize the attribute count to count to zero: We can easily 
accomplish this by having a constructor initialize count to zero as follows: 
 public Count()
{
 count = 0;
 } 
On the other hand, we might want to pass an initialization parameter that allows count to be 
set to various numbers: 
 public Count (int number){
 count = number;
 } 

the above content is taken from the The Object-oriented Thought Process

No comments:

Post a Comment