UOB - Objects: Instances of Class (Lesson)
Objects: Instances of Class
Introduction
If you wanted to bake flower cookies for Earth day you could easily roll out dough and then get crafty with a butter knife to try to cut out a flower cookie. Of course, it would be a lot easier if you had a flower shaped cookie cutter. In Java, the cookie cutter is the class and the cookie is the object. We will eventually learn how to write a class (the cookie cutter) which is the blue print for any objects (cookies) that are created. For now, let’s focus on instances of a class…that means the cookies. Yum!
Objects
Previously we learned data types int, double, and boolean. Those are great but not everything can be defined by a primitive data type. What if you want to make a pet class for a game you create? A pet is certainly not going to be a numerical data type. Enter objects.
The world all around us is full of objects and these objects all have state and behavior. Anything that is not primitive data will be an object. An object has identity (it acts as a single whole). An object has state (it has various properties, which might change). An object has behavior (it can do things and can have things done to it).
Remember, we don’t want to have to go out and create our own paper from trees each time we write something down. We also know it is easier to create holiday cookies using cookie cutters. Anytime we can use something that has already been created it elevates our own creativity and ability to create something else. You may have heard of the saying “don’t recreate the wheel”. Don’t waste time recreating what has already been invented. Use the objects around you – use code that has already been written.
When we think about an object and the class that created it we want to consider what the object looks like and what it does. Check out the example below of a Car class:
What does a Car look like (attributes) and what does it do (methods)?
Attributes | Methods |
color | move forward |
size | move backward |
year make | honk |
model | accelerate |
turn left | |
turn right |
Click on "Runestone Academy" below to open the required reading that is listed.
Runestone Academy: AP CSA – Java Review Links to an external site.:
READ: 2.2 – What is a Class and an Object?
Click on "Introduction to Computer Science Using Java" below to open the required reading that is listed.
Introduction to Computer Science Using Java Links to an external site.:
READ: Chapter 40: Objects
Which came first? The Object or the Class?
Click below to begin the Primitive versus Object Activity.
Drag the term in the top rectangle to the proper choice: Objects or Primitive. Click the checkmark at the bottom right when you have made your selection in order to check your answer.
IMAGES CREATED BY GAVS or OER