Ch 6 A First Look At Classes
What is the difference between a class and an instance of a class?
A class is a collection of programming statements that specify the attributes and methods that a particular type of object may have. It is a "blueprint" that describes an object. An instance of a class is an actual object that exists in memory.
A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses?
A class is generally called as the blueprint of an object. A class contains the fields to hold data and the methods to perform the operations on data. These fields and methods in the class are called as class members. A constructor is one of the methods in a class. The name of the constructor should be the same as the name of the class. The respective constructor is called automatically when an object is created. Usually, constructors perform the initializations for the instance variables or fields in the class. Here, the blueprint represents a set of features that each house will have. From the above definitions, when an object is created in a class, it's corresponding constructor is called automatically. Then the constructor constructs initially a set of identical houses. Hence, the classes are analogous to the blueprint.
What is an accessor method?
A method that gets a value from a class's field but does not change it.
What is a mutator method?
A method that stores a value in a field or changes the value of a field in some other way.
This is a collection of programming statements that specify the fields and methods that a particular type of object may have
Class
Why are constructors useful for performing "start-up" operations?
Constructor is a method that is automatically called when an object of a class is created. They provide initial values in the instance fields.
A class may or may not have more than one constuctor
False
To find the classes needed for an object-oriented application, you identify all the verbs in a description of the problem domain.
False, identify all the nouns. Each of these is a potential class.
When you write a constructor for a class, it still has the default constructor that Java automatically provides
False, if you don't provide constructor that is when Java provides it automatically.
Under what circumstances does Java automatically provide a default constructor for a class?
If you do not write a constructor in a class then Java automatically provides a constructor when the class is compiled.
Assume a program named MailList.java is stored in the Database folder on your hard drive. The program creates objects of the Customer and Account classes. Describe the steps that the compiler goes through in locating and compiling the Customer and Account classes.
Initially, it looks for current folder or directory for the file Customer.class. If that file does not exist, the compiler searches for the file Customer.java and compiles it. This creates the file Customer.class, which makes the Customer class available. Similarly, compiler searches for the Account class.
When the same name is used for two or more methods in the same class, how does Java tell them apart?
Java uses method signature to distinguish it which consists of method's name and data types of method's parameters in the order they appear
If a class has a private field, what has access to the field?
Methods that are members of the same class can access private fields of the class.
How does method overloading improve the usefulness of a class?
Overloaded methods are much more flexible than if we provide one way to perform every operation.
What do you call a constructor that accepts no arguments?
The constructor with no arguments is called a default constructor.
What is the purpose of the new keyword?
This keyword is used to create a new object, or instance of a class
The occurrence of a string literal in a Java program causes a String object to be created in memory, initialized with the string literal
True
Each instance of a class has it's own set of instance fields.
True, by the definition of instance fields
Is it a good idea to make fields private?
Yes, private field declaration indicates that these variables may not be accessed by statements outside the class. The member can be accessed only by methods of the same class.
This is a method that gets a value from a class's field, but does not change it
accessor
The process of matching a method call with the correct method is known as
binding
This is a method that is automatically called when an instance of a class is created
constructor
A class is analogous to a
cookie cutter
This is automatically provided for a class if you do not write one yourself
default constructor
This is a class member that holds data
field
An object is an
instance
This is a method that stores a value in a field or in some way changes the value of a field
mutator
This keyword causes an object to be created in memory
new
When a local variable has the same name as a field, the local variable's name does this to the field's name
shadows
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?
stale
Two or more methods in a class may have the same name, as long as this is different
their parameter lists
A class's responsibilities are
things that the class knows, and actions that the class performs