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.
Which of the following scenarios is best suited for using an accessor method within a class?
Retrieving the balance of a bank account without allowing direct modification.
Calculating the total after applying tax to a product's price.
Changing the password associated with a user account.
Adding a new item to an inventory list in a shopping application.
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.
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

How are we doing?
Give us your feedback and let us know how we can improve
Considering that some programming contexts necessitate adherence strictly adhering Object-Oriented Design Principles, which technique following implementation could accidentally leak references thereby violating encapsulation when designing read-only properties?
Providing direct access via getter methods to cloneable objects' fields without cloning first.
Adhering to the Open/Closed Principle by disallowing any changes made to fields after instantiation unless absolutely necessary for secure maintenance purposes.
Utilizing defensive copying techniques for both getting and setting composite mutable objects, avoiding exposure of internals.
Encapsulating all field mutable types by wrapping them with Immutable counterparts and providing only getters for those versions.
How does implementing lazy instantiation in an accessor method affect its performance when requesting an object for the first time?
No impact since object retrieval is independent of initialization process
Performance varies randomly with each request
Faster as it avoids unnecessary instantiation until needed
Slower due to additional checks and possible instance creation
If a class UserProfile
has a private instance variable Date birthdate
, which of these accessor method signatures ensures safe read-only access in compliance with best practices?
public Date getBirthdate() { return (Date)birthdate.clone(); }
public long getBirthdateInMillis() { return birthdate.getTime(); }
public Date getBirthdate() { return birthdate; }
private Date exposeBirthdate() { return birthdate; }