Writing Classes in AP Computer Science A
What advantage does using accessor methods provide when comparing a superclass-subclass relationship via inheritance versus utilizing separate classes linked through composition?
Supercalsses expose helper methods to reduce code duplication whereas composed objects might lead to the same functionality being reimplemented.
Subclasses can modify inherited fields directly if public or protected which isn't possible when using separate composed objects requiring mutator calls.
Both design choices equally support loose coupling between components since accesor metods prevent direct field manipulation regardless of inheretance or composition use.
Encapsulation is maintained when accessing superclass fields indirectly through accessor methods rather than directly exposing them even in related subclasses via inheritance or unrelated ones through composition.
In Java, if a field balance
is declared as private double balance;
, which accessor method signature would be correct?
public double setBalance()
public void getBalance()
private double getBalance()
public double getBalance()
If a programmer decides to have Class B contain an instance of Class A instead of extending it, what design principle are they likely favoring?
Favoring composition over inheritance in order to increase modularity and flexibility.
Attempting to implement all methods defined in Class A as static, for ease of access without instantiation.
Seeking to override all methods from Class A in Class B, ensuring distinct behaviors across both classes.
Preferring polymorphism so that Class B can assume multiple forms through various subclass implementations.
In Java, what should an accessor method return if it's retrieving a boolean instance variable named 'isAvailable'?
The address location of 'isAvailable'.
The opposite state of 'isAvailable'.
A boolean value indicating the availability status.
A string that says "true" or "false".
In a class where 'items' is an ArrayList storing inventory items, what would be expected from its associated getItemsCount() accessor method?
Sorting 'items' based on quantity before returning total amount present when stocks are disorganized.
Removing all out-of-stock items from 'items' before returning count if inventory needs updating oftenly.
Adding new items into 'items' after counting existing ones each time called upon.
Returning the number of items currently stored in 'items'.
In Java, which return type would be most appropriate for an accessor method that needs to provide access to a private boolean field named 'isActive'?
int
String
void
boolean
When designing a new class for an application, why should you include accessor methods even if all current uses only modify object state?
Future requirements might need read-only access without altering the state.
It guarantees that subclass instances will automatically override these method implementations.
The Java Virtual Machine optimizes execution time when both types of methods are present.
All classes must have both accessor and mutator methods by convention only.

How are we doing?
Give us your feedback and let us know how we can improve
What is the primary purpose of an accessor method in Java?
To create a new instance of an class.
To return the values of an object's variables.
To modify the values of an object's variables.
To turn an instance variable into a static variable.
Which modifier typically accompanies an accessor method in Java?
public
protected
private
static
Following the naming convention for accessor method, what should the name of the method that returns the value of an instance variable named "count" be?
changeCount()
setCount()
modifyCount()
getCount()