Class Notes (836,564)
Canada (509,855)
IAT 265 (12)

IAT265 Objects.docx

6 Pages
Unlock Document

Interactive Arts & Tech
IAT 265
Matt Lockyer

Objects Basics Objects refer to concrete ideas such as a particle or a bicycle or an abstract ideas such as a physics solver or a transport simulation class Bicycle { Fields-store data in objects When an object contains some data, we say that the object encapsulates data int xPos; int yPos; Constructor-special type of method which has an identical name as the class with no return types (makes an object) Bicycle() { xPos = 0; yPos = 25; } Overloaded constructor-same name as the constructor but with different parameters Bicycle(int xPos, int yPos) { // this keyword refers to the instances of the class itself this.xPos = xPos; this.yPos = yPos; } Methods-gives objects the ability to do something void moveRight () { xPos = xPos + 5; } } The new keyword a reference to the object is created. It calls the constructor and returns the address of the new object in memory The newly created object is an instance Bicycle myBike = new Bicycle(); Usage Declaring/Initalizing Once an object is defined by a class definition, it can be instantiated, bringing an instance of that object into your program, allowing you to change fields and call methods PinkElephant myPet; //declare a reference variable of type PinkElephant myPet = new PinkElephant(); Creates a new instance of the PinkElephant class by calling the constructor in the PinkElephant class A reference to the newly created object is returned to where the constructor was called using the new keyword The reference of the newly created object is assigned to the myPet reference variable Once you have reference to the type PinkElephant, you can change some of this objects fields or call some of it's methods myPet.size = 50; // set the size of myPet (a PinkElephant) to 50 myPet.eatPeanuts(); // tell the myPet object to eatPeanuts(); Array Objects PinkElephant[] myPets; // declare an array of PinkElephant objects myPets = new PinkElephant[5]; //initialize the array of PinkElephant objects However, no instances of PinkElephant have been created yet Only the array has been initialized, each position is still empty // initialize each position in our array with a new instance of the PinkElephant class for (int i = 0; i < myPets.length; i++) { myPets[i] = new PinkElephant(); } // call draw method for (int i = 0; i < myPets.length; i++) { myPets[i].draw(); } ArrayList Objects ArrayList myPets; // declare an ArrayList myPets = new ArrayList(); // initialize the ArrayList // add new instances of the PinkElephant class to ArrayList for (int i = 0; i < 5; i++) { myPets.add ( new PinkElephant() ); } // draw PinkElephants inside the ArrayList myPets for (int i = 0; i < myPets.size(); i++) { PinkElephant pinkE = (PinkElephant) myPets.get(i); pinkE.draw(); } Typed ArrayList ArrayList myPets = new ArrayList(); myPets.add( new PinkElephant() ); myPets.get(0).draw(); A newly created ArrayList has a size of 0 ArrayLists do not remember the type of objects they contain, so each object reference obtained using the .get(index) method must be cast before using A typed ArrayList will always return a reference of the type specified when the ArrayList was created, making life much more simple Inheritance Extending a class Add the word extends and the name of the class the new class should extend. This new class is now a subclass of the class which it extends class RaceBike extends Bicycle { //The RaceBike constructor RaceBike() { } } Why extend a class Helps add complexity to a program without requiring the programmer to rewrite a lot of code E.g. Bicycle Class There is a general Bicycle object, and then there are a few specific Bicycle objects. Each specific Bicycle object extends the general Bicycle class, and therefore is a subclass of the Bicycle class When to extend a class When creating a new object a lot of the fields and methods are repeated that belong to another, similar object. You might want to "abstract" out the common properties of both classes, and put these into a parent class. Now each object from before can inherit these common fields and methods, and there's no need to code them for each object E.g. Bicycle Class Every bicycle needs a position, wheel rotation, gear, etc. But only a mountain bike would need shock suspension. And perhaps only a
More Less

Related notes for IAT 265

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.