π2.1: Objects
Table of Contents
π This page is a condensed version of CSAwesome Topic 2.1
Using a GitHub Template for class notes
- Go to the public template repository for our class: BWL-CS Java Template
- Click the button above the list of files then select
Create a new repository
- Specify the repository name:
CS2-Unit-2-Notes
- Click
Now you have your own personal copy of this starter code that you can always access under the
Your repositories
section of GitHub! - Now on your repository, click and select the
Codespaces
tab - Click
Create Codespace on main
and wait for the environment to load, then youβre ready to code! - π Take notes in this Codespace during class, coding along with the instructor.
Objects are Instances of Classes
Java is an object-oriented programming language. That means that one of the primary ways of organizing our programs is in terms of objects.
Objects are a kind of value that combines data, and code that operates on that data, into a single unit. Objects are defined in Java by writing classes which provide a blueprint for creating objects of a certain kind, describing the data and code that all instances of that class have.
Sometimes classes and objects are used to model things in the real world, such as if we made a Student
class to represent students in a school. Other times classes are just ways of organizing different parts of our programs that may not correspond to anything in the world outside the computer.
But in Java all programs are built out of classes. This is why, as you saw in Unit 1, every Java program starts with public class
: the first thing we have to do when we write a Java program is define at least one class.
What are Objects and Classes?
Objects are values created by constructing an instance of a class. This unit focuses on using classes, written by other coders, to create and interact with objects of a certain type. Later on, in Unit 5, youβll learn to write your own classes.
When you think about making objects from a class, you can think of a class like a blueprint or a cookie cutter. It is used to create the cookies (objects) and can be used to create as many cookies (objects) as you want. As a blueprint, each class defines the attributes its objects have (the properties or what each object knows about itself) and the behaviors (what each object can do). In Java code, the attributes are written as instance variables in the class, and the behaviors are written as methods.
You can also think of a class as defining a new, custom data type!
Just like you use int
to declare variables that can hold an whole number value, you can use Turtle
to declare a variable whose value has to be an instance of the Turtle
class.
Just like the Java compiler will only let you do things with the values of
int
variables that make sense (like adding and multiplying them), it will only let you do things with values of aTurtle
variable that make sense to do with turtles, namely accessing the instance variables and methods defined in theTurtle
class.
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.
- π¬ Discuss with your class: What are some attributes of cats? What are some behaviors of cats?
Attributes are often nouns or adjectives describing features
Behaviors are often verbs
Intro to Objects with Turtles
The Turtle
class (which was written for you) is a blueprint for turtle objects. It defines attributes for graphical turtles like their color
and position
, and methods to make the turtles move
. The Java program below creates a Turtle object called yertle using the Turtle class:
public class TurtleTest
{
public static void main(String[] args)
{
World habitat = new World(300, 300);
Turtle yertle = new Turtle(habitat);
yertle.forward();
yertle.turnLeft();
yertle.forward();
habitat.show(true);
}
}
Dot Operator .
The dot operator .
is used to call π£ an objectβs method. You can think of the .
as commanding 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.
Example: to go forward 50 pixels instead of 100, pass the distance argument into the method:
yertle.forward(50);
π¬ Discuss:
- Is
yertle
a class or an object? - Is
Turtle
a class or an instance of a class? - Is
setHeight
an attribute or a behavior?
Turtle Class Diagram
Here is a class diagram that shows some of the attributes and methods in the class Turtle
:
π» In-Class Activity
βοΈ Summary
-
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.
-
An object is a specific instance of a class with defined attributes. Objects are declared as variables of a class type.
- An attribute or instance variable is data the object knows about itself.
- Example: a Turtle object knows the direction it is facing or its color.
- A behavior or method is something that an object can do.
- Example: a Turtle object can go forward 100 pixels.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.