Ch. 6
public
When the public access specifier is applied to a class member, the member can be accessed by code inside the class or outside.
default constructor
this sets all of the object's numeric fields to 0 and boolean fields to false.if the object has any fields that are reference variables, this sets them to the special value null, which means that they do not reference anything.
compiler
this uses the method signature to determine which version of the overloaded method to bind the call to
What does an object use its field for?
to store data
T/F: As a result of passing an object as an argument, the parameter variable references the object, and the receiving method has access to the object.
true
The default constructor is a no-arg constructor. true or false?
true
When an object is created its constructor is always called. True or false?
true
true or false. each instance of a class has its own set of instance fields
true
true or false. the new operator creates an instance of a class
true
Finding the class
1. Get a written description of the problem domain 2. identify all nouns each, is a potential class 3. Refine list to include only classes relevant to the problem
what are the two steps required to create an object?
1. declare a reference variable 2. create the object in memory, and assign its memory address to the reference variable
steps for creating an object
1. you declare a reference variable 2. you create the object in memory, and assign its memory address to the reference variable
What does the "new" operator do?
the new operator creates an object in memory and returns the object's memory address
java.lang package
the package is automatically imported into every java program
binding
the process of matching a method call with the correct method
What is not part of the method signature?
the return type
instance fields
variables declared as in a class can be accessed by any instance method in the same class as the field
How do you identify the potential classes in a problem domain description?
Identify all the nouns and pronouns in the problem domain description. Each of these is a potential class. then refine the list to include only the classes that are relevant to the problem.
problem domain
the set of real world objects, parties, and major events related to the problem
Why is data hiding important?
because classes are typically used as components in large software systems, involving a team of programmers; helps enforce the integrity of an object's internal data
Why is method overloading important?
because sometimes you need several different ways to perform the same operation
this is automatically provided for a class if you do not write one yourself a. accessor method b. default instance c. default constructor d. variable declaration
c. default constructor
This is a class member that holds data. a. method b. instance. c. field d. constructor
c. field
Two or more methods in a class may have the same name, as long as this is different. a. their return values b. their access specifier c. their parameter lists d. their memory address
c. their parameter lists
class
code that describes a particular type of object
class
code that describes a particular type of object, it specifies the data that an object can hold (the object's field), and the actions that an object can perform (the object's methods). serves as a "blueprint" that can be used to create a particular type of object
signature
consists of method's name and the data types of the method's parameters in the order that they appear
method signature
consists of the method's name and the data types of the method's parameters, in the order that they appear
what is a constructor's return type?
constructor has no return type, not even void
How is a constructor named?
constructor has the same name as the class
What does the key word "new" do?
creates an instance of an object in memory
an object is a(n) _________. a. house b. blueprint c. drafting table d. architect
d. architect
A class's responsibilities are ______-. a. the objects created from the class b. the things the class knows c. actions the class performs d. both b and c
d. both b and c
This is a method that stores a value in a field or in some other way changes the value of a field. a. accessor b. constructor c. void d. mutator
d. mutator
fields
data stored in an object
instance
each object that is created from a class
true or false. A class may not have more than one constructor.
false
true or false. to find the classes needed for an object-oriented application, you identify all of the verbs in a description of the problem domain
false
true or false. when you write a constructor for a class, it still has the default constructor that Java automatically provides
false
members
fields and methods that belong to a class
A common layout is:
fields listed first. methods listed second.
special properties of constructors
have the same name as the class. have no return type (not even void). may not return any values. are typically public.
explicit import statement
identifies the package location of a single class Ex.) import java.util.Scanner;)
You have programs that create Scanner, Random, and PrintWriter objects. Where are the Scanner, Random, and PrintWriter classes?
in the Java API
What values do reference variables hold?
it holds an object's memory address. The reference variable doesn't hold an actual piece of data that your program will work with. We say that the variable references the object. When you want to work with the object, you use the variable that references it.
How is a class like a blueprint?
it is a code that can be used to create a particular type of object
How to avoid stale data?
it is best to calculate the value of that data within a method rather than store it in a variable
Scanner keyboard = new Scanner(System.in); What is the program doing with this line of code?
it's creating an instance of the Scanner class, and assigns the object's address to a variable named keyboard. the object will be used to read keyboard input
overloaded
multiple methods in the same class have the same name but use different types of parameters
method overloading
multiple methods in the same class have the same name, but use different types of parameters
overloaded
multiple methods in the same class have the same name, but use different types of parameters
PrintWriter outputFile = new PrintWriter("numbers.txt"); What is happening on the right side of the equal sign and what is happening on the left side of the equal sign?
on the right side, the new operator creates a PrintWriter object in memory. On the left side, the object's memory address is assigned to the "outputFile" variable.
What are an object's methods?
operations that a method can perform
methods
operations that an object can perform
binding
process of matching a method call with the correct method
Unified Modeling Language (UML)
provides a set of standard diagrams for graphically depicting object-oriented systems
problem domain
set of real-world objects, parties, and major events related to the problem
package
simply a group of related classes.
wild card import statement
tells the compiler to import all of the classes in a package
wildcard import
tells the compiler to import all of the classes in a package
wildcard import statement
tells the compiler to import all of the classes in a package Ex.) import java.util.*;
"import" statement
tells the compiler which package a class is located in
default constructor
the constructor that Java provides
default constructor and what it does
the constructor that Java provides; has no parameters, used to initialize an object in a default configuration sets all of the object's numeric fields to 0. sets all of the object's boolean fields to false. sets all the object's reference variables to the special value null.
members
the fields and methods that belong to a class
When a variable is said to reference an object, what is actually stored in the variable?
the memory address of the object.
What two questions should you ask to determine a class's responsibilities?
"in the context of this problem, what must the class know? What must the class do?"
How many default constructors may a class have?
A class may have only one constructor
A string literal, such as "Joe", causes what type of object to be created?
A string object. String name "Joe";
You hear someone make the following comments: "a blueprint is a design for a house. A carpenter can use the blueprint to build the house. If the carpenter wishes, he or she can build several identical houses from the same blueprint." Think of this as a metaphor for classes and objects. Does the blueprint represent a class, or does it represent an object?
Classes are the blueprints. A class is the blueprint for an object. It species the fields and methods a particular type of object has.
When designing an object-oriented application, who should write a description of the problem domain?
If you adequately understand the nature of the problem you are trying to solve, you can write a description of the problem domain yourself. If you do not thoroughly understand the nature of the problem, you should have an expert write the description for you.
How is the relationship between an object and a reference variable similar to a kite and a spool of string?
In order to fly a kite, you need a spool of string attached to it. When the ite is airborne, you use the spool of string to hold on to the kite and control it. this is similar to the relationship between an object and the variable that references the object. think of the object as the kite, and the reference variable as the spool of string.
method's signature
Java uses this to distinguish it from other methods of the same name. It consists of the method's name and the data types of the method's parameters, in the order that they appear.
Will all of a class's actions always be directly mentioned in the problem domain description?
No, often responsibilities are discovered through brainstorming.
Examples of classes from which you've created objects provided by the Java API
Scanner Random PrintWriter
What are two general capabilities of an object?
Storing data (fields). Performing operations (methods).
What are two capabilities of objects?
Storing data - data are called fields. Performing operations - operations are called methods.
In this chapter we used the metaphor of a kite attached to a spool of string to describe the relationships between an object and a reference variable. In this metaphor, does the kite represent an object, or a reference variable?
The kite represents the object. The relationship between a reference varaible and object is like a spool of string and a flying kite.
What are the class's responsibilities?
Things that the class is responsible for knowing and the actions that the class is responsible for doing.
True or False: When an object is created, its constructor is always called.
True
access specifier
a Java keyword that indicates how a field or method can be accessed
What is a class?
a code that describes a particular type of object; it specifies the data that an object can hold (the object's fields), and the actions that an object can perform (the object's methods)
no-arg constructor
a constructor that does not accept arguments
no-arg constructor
a constructor that does not accept arguments Ex.) Java's default constructor
package
a group of related classes
accessor method
a method that gets a value from a class's field but does not change
accessor method
a method that gets a value from a class's field but does not change it
constructor
a method that is automatically called when an instance of a class is created. They normally perform initialization or setup operations, such as storing initial values in instance fields. They help construct an object.
constructor
a method that is automatically called when an object is created; used to perform operations at the time an object is created; typically initialize instance fields and perform other object initialization tasks
mutator method
a method that stores a value in a field or changes the value of a field in some other way
object
a software component that exists in memory and serves a specific purpose in a program; created from a class that contains code describing the object
ClassAct(int number) { item = number; } a)What is the name of the class that this constructor appears in? b)Write a statement that creates an object from the class and passes the value 25 as an argument to the constructor.
a) ClassAct b)ClassAct object = new ClassAct(25);
This is a method that gets a value from a class's field, but does not change it. a. accessor b. constructor c. void d. mutator
a. accessor
This is a collection of programming statements that specify the fields and methods that a particular type of object may have. a. class b. method c. parameter d. instance
a. class
When a local variable has the same name as a field, the local variable's name does this to the field's name. a. shadows b. complements c. deletes d. merges with
a. shadows
data hiding
an important concept in object-oriented programming
What is an instance?
an object created from a class
data hiding
an object hides its internal data from code that is outside the class that the object is an instance of
The process of matching a method call with the correct method is known as _______. a. matching b. binding c. linking d. connecting
b. binding
A class is analogous to a(n)_____-. a. house b. blueprint c. drafting table d. architect
b. blueprint
This is a method that is automatically called when an instance of a class is created. a. accessor b. constructor c. void d. mutator
b. constructor
this key word causes an object to be created in memory. a. create b. new c. object d. construct
b. new
When the value of an item is dependent on other data, and that item is not updated when the other data is changed, what has the value become? a. bitter b. stale c. asynchronous d. moldy
b. stale
shadowing
when a method has a local variable with the same name as an instance field. it's discouraged and local variable names should not be the same as instance field names
instance method
when a method is designed to work on an instance of a class
uninitialized reference variable
when the box variable does not yet hold an object's address
private
when the private access specifier is applied to a class member, the member cannot be accessed by code outside the class. The member can be accessed only by methods that are members of the same class
private
when the private access specifier is applied to a class member, the member cannot be accessed by code outside the class. The member can be accessed only by methods that are members of the same class.
public
when the public access specifier is applied to a class member, the member can be accessed by code inside the class or outside
stale
when the value of an item is dependent on other data and that item is not updated when the other data is changed
stale data
when the value of an item is dependent on other data and that item is not updated when the other data is changed
method overloading and why it's important
when two or more methods in a class may have the same name as long as their parameter lists are different. it's important because sometimes you need several different ways to perform the same operation
object's memory address
when you pass an object as an argument, the thing that is passed into the parameter variable
Are UML diagrams language independent?
yes
Can reference variables be declared without being initialized?
yes