Skip to main content
Contents Index
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 6.6 Objects - Instances of Classes
Subsection 6.6.1 What are Objects and Classes?
Java is an
object-oriented programming language that can be used to model objects in the real world. We’ve seen that Java programs start with
public class. A
class is used to define a new data type or a blueprint for objects.
Objects are the variables created from a
class definition, and they are
instances of 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.
You can think of a class as the type or classification. Each type can have
attributes (the object’s properties or what it knows about itself) and
behaviors (what an object does). In Java code, the attributes are written as
instance variables in the class, and the behaviors are written as
methods .
when we declare a variable like String name;, we are creating a String object using the built-in String class, which provides predefined methods for handling text.
We can define and use our own classes, such as Person, Monster, Student, where we specify attributes and behaviors tailored to our specific needs.
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,
.toUppoerCase() asks the
String object to create a new string by converting all of the characters in this String to upper case.
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 the
equals(...)
Subsection 6.6.2 Practice
Checkpoint 6.6.1 .
Checkpoint 6.6.2 .
2-1-4: How many objects can you create from a class in Java?
1
There is one definition of a class, but the class can create as many objects as are needed.
10
There is no limit on the number of objects you can create from a class.
1000
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.
Checkpoint 6.6.3 .
2-1-5: 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.
Checkpoint 6.6.4 .
2-1-6: 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.
Checkpoint 6.6.5 .
2-1-18: A student has created a Dog class. The class contains variables to represent the following.
A String variable called
breed to represent the breed of the dog
An int variable called
age to represent the age of the dog
A String variable called
name to represent the name of the dog
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.
Checkpoint 6.6.6 .
2-1-19: 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.