Chapter 6 Flashcards
What is an object in Java?
An object is an entity in memory that performs specific tasks. It can:
Store data (fields).
Perform operations (methods).
Name two capabilities of objects in Java.
Objects can store data (fields).
Objects can perform operations (methods).
What is a class in Java?
A class is a blueprint for creating objects. It specifies:
The data an object can hold (fields).
The actions an object can perform (methods).
What is an instance of a class?
An instance is a specific object created from a class.
Give examples of objects you’ve used in Java.
Scanner: Reads input from users.
Random: Generates random numbers.
PrintWriter: Writes data to files.
What are the fields in a Rectangle class?
length: Holds the rectangle’s length.
width: Holds the rectangle’s width.
What are the methods in a Rectangle class?
setLength: Sets the length.
setWidth: Sets the width.
getLength: Returns the length.
getWidth: Returns the width.
getArea: Calculates and returns the area (length * width).
What does UML stand for?
Unified Modeling Language. It is used to visually represent object-oriented systems.
What are access specifiers in Java?
Java keywords that control how fields or methods can be accessed.
What does the public access specifier do?
A public member can be accessed by any code, inside or outside the class.
What does the private access specifier do?
A private member can only be accessed by methods within the same class, protecting it from external access.
What is the purpose of the set and get methods in a class?
set methods assign values to fields.
get methods retrieve values from fields, ensuring controlled access.
What does an object need to perform its actions?
An object needs a class as a blueprint to define its fields and methods.
How many objects can be created from a single class?
As many as needed; each object is an independent instance of the class.
Why are access specifiers important?
They help encapsulate and protect data within a class, controlling how it can be accessed and modified.
What is Data Hiding?
Data hiding means fields in a class are private and cannot be directly accessed outside the class.
Access is controlled via public methods (accessors and mutators).
What is an Accessor?
A method that allows acess to the value of a private field.
Example: public double getLength()
What is a Mutator?
A method that modifies the value of a private field.
Example: public void setLength(double len)
How does Data Hiding ensure integrity?
Only class methods can modify an object’s private fields, reducing unintended changes.
External code must use public methods, ensuring controlled updates.
What is the purpose of dynamic calculations in methods?
Avoids stale data by calculating values (e.g., length × width) dynamically in a method.
Example:
java
Copy code
public double getArea() {
return length * width;
}
What is UML?
Unified Modeling Language (UML) provides language-independent diagrams for:
Fields
Methods
Access modifiers
Return types
What are Instance Fields and Methods?
Instance Fields: Each object gets its own copy of these fields.
Instance Methods: Operate on instance fields and require an object to be created.
What is a Constructor?
A special method that initializes fields and is automatically called when an object is created.
Example:
java
Copy code
public Rectangle(double len, double w) {
length = len;
width = w;
}
What is a Default Constructor?
Provided by Java if no constructor is written.
Sets numeric fields to 0, boolean fields to false, and object references to null.
What is a No-Arg Constructor?
A constructor with no parameters.
Example:
java
Copy code
public Rectangle() {
length = 1.0;
width = 1.0;
}
What happens if a reference variable is uninitialized?
It must reference an object before being used, or a compiler error will occur.
Example:
java
Copy code
Rectangle box;
box = new Rectangle(7.0, 14.0);
What is Method Overloading?
Multiple methods in a class with the same name but different parameter lists.
Example:
java
Copy code
public void add(int a, int b) { }
public void add(String a, String b) { }
What is a Method Signature?
The method’s name and the data types of its parameters (order matters).
Return type is not part of the signature.
What is Shadowing?
A local variable in a method has the same name as an instance field, hiding the field’s value.
Shadowing is discouraged to avoid confusion.
How are String objects created?
Using a constructor:
java
Copy code
String name = new String(“Michael Long”);
Shorthand:
java
Copy code
String name = “Michael Long”;
What happens when an object is passed as an argument?
The object’s memory address is passed to the parameter.
The receiving method can access and modify the object.
What are Explicit and Wildcard Imports?
Explicit Import: Imports a specific class.
java
Copy code
import java.util.Scanner;
Wildcard Import: Imports all classes in a package.
java
Copy code
import java.util.*;
What are Responsibilities in Class Design?
Knowledge: What the class knows (fields).
Behavior: What the class does (methods).
What is the Java Default Package?
The java.lang package is automatically available in any Java program.
What is the purpose of Method Binding?
The compiler matches a method call with the correct overloaded method using the method signature.