Chapter 3: Implementing Classes Flashcards
Where does an object store its data?
an instance variable
What is an instance of a class?
an object of the class
What are the 3 parts that an instance variable declaration consist of?
1.) an access specifier (private)
2.) The type of the instance variable
3.) The name of the instance variable
Write a code for the click method, which advances the counter value by 1
public void click()
{
value = value + 1
}
Write the getValue method
public int getValue()
{
return value;
}
T/F: Private instance values can only be accessed by methods of the same class.
True
What is encapsulation?
the process of hiding implementation details and providing methods for data access
What does encapsulation allow a programmer to do?
use a class without having to know its implementation
What does information hiding make it easier to do?
locate errors and change implementation
What do you need to know before implementing a class
which methods are required
What do constructors do?
set the initial data for objects
What is the name of a constructor?
the name of the class
What are constructors return type?
trick question: constructors have no return type (not even void)
What is the name for a constructor that takes no argument
a no-argument constructor
What is the following an example of?
public BankAccount()
a no-argument constructor
Where do you place constructors and method declarations?
inside a class
What are documentation comments used for?
to describe the classes and public methods of your program
What is the @param tag used for
methods with no arguments
What is the @return tag used for
methods whose return type is void
What does the private implementation of a class consist of?
instance variables and the bodies of constructors and methods
What is the simple job of a constructor?
to initialize the instance variables of an object
What does a unit test do?
verify that a class works in isolation
What are the two ways to test a class?
use an environment for interactive testing, or write a tester class to execute test instructions
What is a local variable?
a variable that is declared in the body of a method
Where are parameter variables declared?
in the method header
What happens when a method exits?
its local variables are removed
Where do instance variables belong to?
objects (not methods)
T/F: You must initialize local variables
True
T/F: You must initialize instance variables
False
What is the implicit parameter?
the use of the instance variable in a method
What reference denotes the implicit parameter?
this
T/F: An instance variable shadows a local variable with the same name
False - a local variable shadows an instance variable with the same name.