π1.12: Java Objects
Table of Contents
π This page is a condensed version of CSAwesome Topic 1.12
Objects β Instances of Classes
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 template for creating the objects.
In Java, all programs are built out of classes. This is why every Java program starts with the keywords
public class
.
πββ¬ Letβs say you wanted to create a virtual model of a cat cafΓ©, populated with tons of kitties of all sizes and colors. You can start by writing a Cat
class that defines a generic cat object. To do this, you need to think very generally about what cats are, what characteristics they have, and what they can do.
π¬ Discuss:
- What are some common attributes of cats?
- What are some behaviors?
In this unit, you will learn the vocabulary of object-oriented programming and how to create and use objects of a class written for you. In later units, you will learn to write your own classes.
Classes & Objects
A class is a template for a specific kind of object (of a particular classification). You can also think of a class as defining a custom data type.
Real-life analogies:
- A blueprint of a house used to construct multiple homes
- A cookie cutter used to create cookies
The cookie cutter (class) can be used to create as many cookies (objects) as you want. The cookies are the same shape, but can have different decorations (attribute values).
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.
Example:
// Declaring 2 Turtle-type objects called yertle and myrtle
Turtle yertle;
Turtle myrtle;
Video: Classes & Objects
Attributes & Behaviors
A class defines:
- Attributes/Fields
- Data the object βknowsβ (e.g.,
name
,age
,color
- Behaviors/Methods
- Actions the object βdoesβ (e.g.,
meow()
,eatTreats()
) or what can be done to it.
Video: Attributes & Behaviors
Example: The Belt
class described in the video below has 3 instance variables (attributes), and each belt object can have different values for them.
CSAwesome Activities: Turtle Class
A class like Turtle
defines the data (attributes/fields) and behaviors/methods shared by all turtle-type objects you create.
This diagram of a turtle shows some of the generic
Turtle
attributes likename
,width
,height
,color
in the body of the turtle and its methods likeforward()
,backward()
, written around the turtle.
π’ To explore the concepts from this lesson in code, weβll play around with the Turtle
class on the CSAwesome website instead of taking notes.
π GO TO: , SIGN IN to your account, and complete all the turtle-related coding activities/challenges with a partner.
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) All classes in Java are subclasses of the Object class.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.