Jan 13, 2008

Giving Objects Personality

Today we talk more about objects! Yay! Specifically how they are constructed. Last time we made ourselves a computer object. If you recall the line that instantiated [made] the object looked like this:

Computer myComputer = new Computer();

and follows the format:

ClassName objectName = new ClassName();

Now Computer() is called a constructor. This looks and acts a lot like a method but it isn't! It is a constructor! A constructor is the code that runs when a new object is instantiated [made], it customizes your object for you if you give it the proper information and have a constructor made to take that information. Now we didn't make a constructor for our last program but the compiler made one for us! The compiler automatically made the constructor for us because we didn't make a constructor ourselves and it looks something like this:

public class Computer{
public Computer(){
}
}

Now that's not a very good constructor, it doesn't give the object any attributes. In our last program we used methods to change instance variables we'll use a constructor to set a few new ones in this case.

Note: Notice how constructors are different then methods! First constructors have the exact name of the class. Second methods have a return type (so far we have only seen void which means no return type).

Now constructors can take in information and use it to change the instance variables of an object or you can just change the instance variables without taking in any information, like a default. Some examples of constructors:

*we'll give our computers some different instances variables for the purposes of this example (all the following code would go within the braces for the Computer class):

int hardDriveSizeInGB;
double processorSpeedInGHz;
int numOfUSBPorts;

public Computer(){
hardDriveSizeInGB = 250;
processorSpeedInGHz = 2.8;
numOfUSBPorts = 3;
}
public Computer(int hardDrive, double processor, int ports){
hardDriveSizeInGB = hardDrive;
processorSpeedInGHz = processor;
numOfUSBPorts = ports;
}
public Computer(int hardDrive)
hardDriveSizeInGB = hardDrive;
processorSpeedInGHz = 2.8;
numOfUSBPorts = 3;
}

Now each of these is different. Which one is called is dependant on what you give the constructor in the declaration.
Computer myComputer = new Computer(300);
This will call our third constructor because it satisfies the needs for it's parameters. Same works for the other two.
Computer myComputer = new Computer();
Computer myComputer = new Computer(300, 4.4, 12);
They will call their respective constructors. But make sure you give the correct parameters for atleast one of your constructors otherwise it'll blow up in your face. Now if we didn't have our first constructor with no arguments the compiler will no longer make it automatically for us. As soon as you have one constructor made it won't bother.

Note: Having more than one constructor for a class is called having overloaded constructors.

Common mistake! You must make constructors with different argument lists. Meaning you can't have two constructors that just take two integers, the compiler doesn't know which one to call and it will blow up.

One last tidbit, there are other things you can do within constructors like verify that what is being put into the constructor is feasible. Take this example:

Computer obsenelyHugeHD = new Computer(20000)

public Computer(int hardDrive)
if (hardDrive <>
hardDriveSizeInGB = hardDrive;
} else {
hardDriveSizeInGB = 250;
}
processorSpeedInGHz = 2.8;
numOfUSBPorts = 3;
}

That's all for constructors for now. There is more though! But we'll cover that another day, I'm tired. If you have any questions leave a comment and I'll clear it up the best I can.