13
1 Objects and Classes: creating simulations Powerpoint created by Beverly McGuire

Objects and Classes: creating simulations

Embed Size (px)

DESCRIPTION

Objects and Classes: creating simulations. Powerpoint created by Beverly McGuire. Objects. Objects are the things that do the action or are acted upon in a simulation. Objects can have data (fields) and behavior (methods). - PowerPoint PPT Presentation

Citation preview

Page 1: Objects and Classes: creating simulations

1

Objects and Classes: creating simulations

Powerpoint created by

Beverly McGuire

Page 2: Objects and Classes: creating simulations

2

Objects Objects are the things that do the action or are

acted upon in a simulation. Objects can have data (fields) and behavior

(methods). Every object is an instance of a class (its

classification or type). The class creates the objects since it knows how

big the object needs to be (how many and what type of fields).

All objects keep a reference to the class that created them (they know their type).

Page 3: Objects and Classes: creating simulations

3

What does this mean?

If you go to a restaurant you may be greeted by the greeter and shown to your table.

The waiter will take your order and bring you your food. The chef will cook the food. If we want to make a

simulation of this we will have objects of the type: Greeter, Waiter, Chef, Customer, Order, Food, and so on.

We can have more than one object of each class. There will usually be several waiters and many

customers.

Page 4: Objects and Classes: creating simulations

4

What are Objects used for?

Classes define the data (fields) that objects of that classification will have and the things that they will be able to do (methods).

Classes create objects. Objects act in the simulation.

Page 5: Objects and Classes: creating simulations

5

Analogies:

Job descriptions are like classes. Workers in those jobs are like the objects. A class is also like a blueprint or cookie

cutter. Objects are like houses built based on the

blueprints or like cookies made with the cookie cutter.

Page 6: Objects and Classes: creating simulations

6

How Create an Object:

You ask the class to create a new object using the new keyword:

new Class(parameter list) You can create a new String object by the

following: String name = “Susan";

Page 7: Objects and Classes: creating simulations

7

An Example of Creating an Object:

new World();new Turtle(new World());

Page 8: Objects and Classes: creating simulations

8

Creating a Class:

Classes are defined in files. Each class is usually defined in its own

file. The file name is the same as the class

name with an extension of ".java".

Page 9: Objects and Classes: creating simulations

9

Creating a Class: (cont.)

You define a class using the class keyword:

visibility class Name

or

visibility class Name extends ParentClass

visibility is usually public meaning all other classes can use it

Page 10: Objects and Classes: creating simulations

10

Creating a Class: (cont.)

Name is the name of the class. By convention each word in a class name starts with an uppercase letter like SimpleTurtle.

ParentClass is the name of the class this class inherits from. If a parent class isn't specified the parent is Object.

Page 11: Objects and Classes: creating simulations

11

Creating a Class Common Errors: Forgetting to declare a variable to hold

a reference to a new object

If you create a new object but don't declare a variable to keep track of the reference you won't have any way to refer to it again. The object will be garbage collected and the memory it uses will be released.

new Turtle(new World());

Page 12: Objects and Classes: creating simulations

12

Creating a Class Common Errors:

Not declaring a class in a file with the same name

Don’t try to save out a class into a new file with a different name. The name of the class and the file must match.

Page 13: Objects and Classes: creating simulations

13

Special thanks to Barb Ericson for providing

Concept Cards for this presentation