Skip to main content
Logo image

Section 1.12 Objects - Instances of Classes

45 minutes
Java is an object-oriented programming language. That means that one of the primary ways of designing and organizing a Java program is in terms of objects. Objects combine data and the code that operates on that data into a single unit. To create objects, we first define a class which provides a blueprint for creating the objects. In Java, all programs are built out of classes. This is why, every Java program starts with public class. In this unit, you will learn the vocabulary of object-oriented programming and to create and use objects of a class written for you. In later units, you will learn to write your own classes.

Subsection 1.12.1 What are Classes and Objects?

You can think of a class as something like a blueprint of a house that is used to construct houses (objects), or like a cookie cutter that is used to create the cookies (objects). The cookie cutter (class) can be used to create as many cookies (objects) as you want. The cookies (objects) are all the same shape and size, but they can have different colors and decorations.
Figure 1.12.1. Using a blueprint (class) to construct houses (objects)
Figure 1.12.2. Using a cookie cutter (class) to make cookies (objects)
You can also think of a class as defining a new data type. In this lesson, we will use the class Turtle to make animated turtle objects. Just like you use int to declare variables that hold numbers, you can use Turtle to declare many variables, animated turtle objects, who are instances of the Turtle class.
// Creating 2 Turtle objects called yertle and myrtle
Turtle yertle;
Turtle myrtle;
Watch the following video
 1 
https://www.youtube.com/watch?v=64DOwDu5SVo&list=PLHqz- wcqDQIEP6p1_0wOb9l9aQ0qFijrP&ab_channel=colleenlewis
by Dr. Colleen Lewis about classes and objects:
Figure 1.12.3. Classes and Objects

Subsection 1.12.2 Attributes and Behaviors

A class defines the attributes (data) and behaviors (methods) that all objects of that class will have. The objects are the specific instances of the class that have their own values for the attributes. Attributes are the data or properties that an object knows about itself, for example, a turtle object’s color and size. Behaviors are the things that an object can do. For example, a turtle object can go forward, turn left, or turn right. The attributes and behaviors of a class are defined in the class, but each object has its own values for the attributes.
This video
 2 
https://www.youtube.com/watch?v=Y9vn6u3901Y&list=PLHqz- wcqDQIEP6p1_0wOb9l9aQ0qFijrP&ab_channel=colleenlewis
shows another class called Belt and how it has 3 instance variables to define its attributes. Every belt object can have different values stored in the instance variables.
Figure 1.12.4. Belt Object Attributes
The following picture has lots of cats (objects of the type cat). They are all different, but they share the same attributes and behaviors that make up a cat. They are all instances of cat with different values for their attributes. Name some of the attributes and behaviors of the cats below. For example, the color ( attribute) of the first cat is black ( attribute value) and it is playing ( behavior).
Figure 1.12.5. Pictures of cats (cat objects)

Activity 1.12.1.

Discuss with your class: What are some attributes of cats? What are some behaviors of cats? (Note that attributes are often nouns or adjectives describing features of cats, and behaviors are often verbs).
The following picture of a turtle shows some of the Turtle attributes like name, width, height, color in the body of the turtle and its methods like forward(), backward(), written around the turtle.
Figure 1.12.6. Turtle Attributes and Behaviors

Activity 1.12.2.

Activity 1.12.3.

How many objects can you create from a class in Java?
  • There is one definition of a class, but the class can create as many objects as are needed.
  • There is no limit on the number of objects you can create from a class.
  • There is no limit on the number of objects you can create from a class.
  • As many as you need
  • You can create as many objects as you need from one class.

Activity 1.12.4.

What specifies the behavior for objects of a class in Java?
  • attributes
  • attributes specify the data that an object keeps track of.
  • methods
  • Methods specify the behavior of all objects of a class.
  • class
  • While the class does specify the behavior of all objects created by that class, what part of a class specifies the behavior?
  • object
  • The object behavior is specified by the methods in the class that created the object.

Activity 1.12.5.

What are the data or properties of an object called?
  • attributes
  • attributes specify the data that an object keeps track of.
  • methods
  • Methods specify the behavior of all objects of a class.
  • class
  • While the class does specify the data that all objects of the class keep track of, what part of the class stores the data?
  • object
  • The object data is stored in the attributes of the object. The attributes are defined in the class.

Subsection 1.12.3 Turtle Class

The Turtle class (that we’ve written for you and hidden on this page) is a blueprint for turtle objects. It defines attributes for graphical turtles like their color and position and methods to make the turtles move. Try the Java program below that creates a Turtle object called yertle using the Turtle class. If the code below does not work or is too slow in your browser, you can also see the Turtle code in action on JuiceMind
 3 
https://play.juicemind.com/dashboard/teams/Mk2wWMTqPkekcxTDWqRn/item/beca9c16-4004-4a4e-b4b0-11593e140808#f5357602-b60b-44c3-be0e-dfb07de2a778
or replit
 4 
replit.com/@BerylHoffman/Java-Swing-Turtle#Main.java
or download the files here
 5 
github.com/bhoffman0/CSAwesome2/raw/main/_sources/Unit1-Using-Objects-and-Methods/TurtleJavaSwingCode.zip
to use in your own IDE.

Activity 1.12.6.

Try clicking the run button below to see what the following program does.
The following video
 6 
https://www.youtube.com/watch?v=TFmmG4_KK8I&list=PLHqz- wcqDQIEP6p1_0wOb9l9aQ0qFijrP&ab_channel=colleenlewis
shows how the program creates a World object called habitat and a Turtle object called yertle in memory.
Figure 1.12.7. Turtle Memory Model
The dot operator (.) is used to run an object’s method. You can think of the (.) as asking the object to do something (execute one of its methods). For example, yertle.forward() asks the turtle yertle to go forward. It doesn’t tell yertle how much to go forward, so it goes forward 100 pixels by default. The parentheses () after a method name are there in case you need to give the method arguments (some data) to do its job, for example to go forward 50 pixels instead of 100 in yertle.forward(50); Try changing the code above to go forward 50 pixels instead and then run it again.

Activity 1.12.7.

In the code below, yertle goes forward and then turns left. Can you change the code to make yertle go forward twice and then turnRight?
A variable like yertle of a reference type like Turtle holds an object reference, which can be thought of as the memory address of that object. Variables that are of a primitive types like int hold just a single number value. But objects are complex and contain a collection of values called attributes, for example the color and the size of the turtle.

Subsection 1.12.4 Creating Turtle Objects

When you write a class like the Turtle class, you can create many objects of that class type. In the code below, two turtle objects are created: yertle and myrtle. You can name your turtle and add in a line like the following in the main method to make it move:
// To create or declare a new object, write:
// ClassName variableName = new ClassName(arguments);
Turtle yourTurtleName = new Turtle(habitat);
yourTurtlename.forward();

Activity 1.12.8.

Can you add another turtle object to the code below?

Subsection 1.12.5 Class Hierarchy and Inheritance

Another important concept in object-oriented programming is inheritance. Although the AP CSA exam no longer covers inheritance, you should know what it means. Inheritance is a way to create a new class that is based on an existing class. The new class, called a subclass, inherits the attributes and behaviors of the existing class, called a superclass. In Java, all classes are subclasses of a superclass called Object.
For example, you could have a superclass called Pet with attributes like name and age and behaviors like eat and sleep. You could then have subclasses like Dog and Cat and Turtle that inherit the attributes and behaviors of the Pet class. Each subclass could add attributes and behaviors specific to that type of pet. For example, a Dog subclass could have an attribute called breed and a behavior called bark.
Figure 1.12.8. Inheritance hierarchy for Pet superclass with the subclasses Dog, Cat, and Turtle

Subsection 1.12.6 Turtle Methods

Let’s practice more with the Turtle class and its methods. Here is a class diagram that shows some of the attributes and methods in the class Turtle.
Figure 1.12.9. Turtle Class Diagram
The Turtle world does not use the Cartesian coordinate system with (0,0) in the middle the screen. Instead, (0,0) is at the top left corner of the screen and x increases to the right and y increases towards the bottom of the screen. Most computer graphics systems use this coordinate system which is a carry over from before computers could display graphics and had to approximate the graphics with text print outs.
Figure 1.12.10. The coordinate (0,0) is at the top left of the Turtle world.

Activity 1.12.9.

The following code uses a turtle to draw the digital number 7 (with just straight lines), but the code is mixed up. Drag the code blocks to the right and put them in the correct order to first draw the line going up (towards the top of the page) and then turn and draw a line to the left to make a 7. Remember that the turtle is facing the top of the page when it is first created. Click on the β€œCheck Me” button to check your solution.
After you put the mixed up code in order above, type in the same code below to make the turtle draw a 7.

Activity 1.12.10.

Can you make yertle draw a 7 by going forward to go up and then left?

Activity 1.12.11.

Can you make yertle draw the digital number 8, as 2 squares on top of each other?

Subsection 1.12.7 Coding Challenge : Draw Letters

Working in pairs, use the area below to have your turtle draw simple block-style letters for your first or last name initials using just straight lines (no curves or diagonals).
It may help to act out the code pretending you are the turtle. Remember that which way you turn depends on which direction you are facing, and the turtle begins facing north (towards the top of the page).
Here are some simple turtle methods that you can use:
You may notice that it is challenging to have your turtle draw with these simple methods. In the next lessons, we will use more complex Turtle methods where you can indicate how many steps to take or what angle to turn that will make drawing a lot easier!

Project 1.12.12.

Have your turtle create a block drawing of the initials of your name. Use straight lines (no curves or diagonals unless you want to try adding arguments to the turtle methods).

Subsection 1.12.8 Summary

  • (AP 1.12.A.1) A class defines a new data type (a classification). It is the formal implementation, or blueprint, of the attributes and behaviors of the objects of that class.
  • (AP 1.12.A.1) An object is a specific instance of a class with defined attributes. Objects are declared as variables of a class type.
  • (AP 1.12.B.1) A variable of a reference type holds an object reference, which can be thought of as the memory address of that object.
  • An attribute or instance variable is data the object knows about itself. For example a turtle object knows the direction it is facing or its color.
  • A behavior or method is something that an object can do. For example a turtle object can go forward 100 pixels.
  • (AP 1.12.A.2) A class hierarchy can be developed by putting common attributes and behaviors of related classes into a single class called a superclass. Classes that extend a superclass, called subclasses, can draw upon the existing attributes and behaviors of the superclass without replacing these in the code. This creates an inheritance relationship from the subclasses to the superclass. Designing and implementing inheritance relationships are outside the scope of the AP Computer Science A course and exam.
  • (AP 1.12.A.2) All classes in Java are subclasses of the Object class.

Subsection 1.12.9 AP Practice

Try these AP practice questions that will help to prepare you for the AP Classroom progress checks.

Activity 1.12.13.

A student has created a Dog class. The class contains variables to represent the following.
The object pet is declared as type Dog. Which of the following descriptions is accurate?
  • An attribute of the name object is String.
  • name is an attribute of the pet object or Dog class.
  • An attribute of the pet object is name.
  • name is an attribute of the pet object or Dog class.
  • An instance of the pet class is Dog.
  • An instance of the Dog class is pet.
  • An attribute of the Dog instance is pet.
  • An attribute of the Dog class is name.
  • An instance of the Dog object is pet.
  • An instance of the Dog class is pet.

Activity 1.12.14.

A student has created a Party class. The class contains variables to represent the following.
  • An int variable called numOfPeople to represent the number of people at the party.
  • A boolean variable called discoLightsOn to represent whether the disco ball is on.
  • A boolean variable called partyStarted to represent whether the party has started.
The object myParty is declared as type Party. Which of the following descriptions is accurate?
  • boolean is an attribute of the myParty object.
  • boolean is the type of an attribute, but not an attribute.
  • myParty is an attribute of the Party class.
  • myParty is an instance of the Party class.
  • myParty is an instance of the Party class.
  • myParty is an object that is an instance of the Party class.
  • myParty is an attribute of the Party instance.
  • An attribute of the Party class is numOfPeople.
  • numOfPeople is an instance of the Party object.
  • An attribute of the Party class is numOfPeople.
You have attempted of activities on this page.