- Tamer Maher in Egypt suggested that I include links to the many game programming sites that I like, these will be included in the next posting of this blog. Thank you Tamer for your input! See his blog at http://TamerMaher.spaces.live.com where he discusses security!
Cy Khormaee and Bradley Jensen who will be joining their student teams to attend the Imagine Cup in Cairo, Egypt! They will be joining my other teammates: Dan Waters, Clint Rutkas, and Diane Curtis.
In the previous blog I said that we would go over how to use abstract classes. I decided to discuss object instantiation from classes
How do you create or instantiate objects?
Now back to something that is entirely on track to using existing code and building code that you can share with others.
A few important points:
- A class and an object are different things.
- A class is a pattern that an object is built on, but it is not an object itself.
- An object is a concrete entity based on a class, and is instance of a class.
- The object is created through a process referred to as construction and instantiation
Objects can be created using the new keyword followed by the name of the class that the object will be based upon.
In C# you create the object from the class pattern by the code, written here where the object is given the same name as the class, in the following line of code, I show you the way that is more professional and easier to utilize:
Avatar Avatar = new Avatar(); //Can be confusing, but used in many examples
A better way to instantiate an object would be to give the instantiated object a separate but clear name:
Avatar BaldGuy = new Avatar(); //Less confusing and more descriptive
When an instance of a class is created (instantiate), a reference to the object is passed back to the programmer (the yellow arrow in the figure). In the example above, BaldGuy is a reference to an object based on Avatar. This reference refers to the new object, but does not contain the object data itself. In fact, you can create an object reference without creating an object at all:
Creating object references that do not reference an object is a bad idea because it will fail at run time which occurs after you compile the program. This is an error that will not show up until you try to use your program.
However, such a reference can be made to refer to an object, either by creating a new object, or by assigning it to an existing object, like this:
Avatar BaldGuyB = new Avatar();
Avatar BaldGuyC = BaldGuyB;
- Two object references (BaldGuyB and BaldGuyC) refer to the same object (Avatar).
- Changes to the object made through BaldGuyB will be reflected in subsequent uses of BaldGuyC.
- Objects based on classes that are referenced (the process described) reference types of classes
- Back to abstract classes and how to use them, differences between base classes, etc.
- What is does Override mean?