zuai-logo

What is the difference between a getter method and a setter method?

Getter: Accesses the value of an instance variable. Setter: Modifies the value of an instance variable.

All Flashcards

What is the difference between a getter method and a setter method?
Getter: Accesses the value of an instance variable. Setter: Modifies the value of an instance variable.
What is the difference between public and private access modifiers?
Public: Accessible from anywhere. Private: Accessible only within the declaring class.
What is the difference between returning a primitive type and returning a reference type in a getter method?
Primitive: Returns a copy of the value. Reference: Returns a copy of the reference to the object.
Compare the purpose of accessor methods and mutator methods.
Accessor methods: Provide read access to object's state. Mutator methods: Provide write access to object's state.
Compare direct access of instance variables vs. using accessor methods.
Direct access: Bypasses encapsulation, can lead to data corruption. Accessor methods: Provide controlled access, maintain data integrity.
Compare the use of `toString()` with getter methods for printing object information.
`toString()`: Returns a formatted string representation of the object. Getter methods: Return specific attribute values.
Compare the effect of returning a primitive data type vs. a reference type from a method.
Primitive data type: Returns a copy of the value. Reference type: Returns a copy of the reference to the object.
Compare the use of accessor methods with and without encapsulation.
With encapsulation: Protects data integrity and allows controlled access. Without encapsulation: Exposes data directly, risking unintended modifications.
Compare the use of public and private access modifiers for instance variables.
Public: Allows unrestricted access from any class. Private: Restricts access to within the same class, promoting encapsulation.
Compare the use of accessor methods and direct variable access regarding code maintainability.
Accessor methods: Allow internal implementation changes without affecting external code. Direct variable access: Requires changes in all places where the variable is used.
What is an accessor method?
A method that allows clients to access the data of an object.
What is a getter method?
An accessor method used to get the value of a specific instance variable.
What is the toString() method?
A method that returns information about an object as a string.
What is a client in the context of accessor methods?
Objects, classes, and users that access the data of an object.
What does '@Override' do?
Indicates that a method is overriding a method in a superclass.
What is 'return by value'?
Returning a copy of the value of a primitive data type.
What is returned when a getter method returns a reference to an object?
A copy of the reference to the object is returned, not a copy of the object itself.
What happens when the 'return' keyword is triggered?
The program immediately returns to the point after whatever called the method.
What is the access modifier of accessor methods?
public
Why are accessor methods made public?
To allow other clients (objects, classes, and users) to access the data of an object.
What are the general steps to write a getter method?
1. Determine the instance variable to access. 2. Define a public method with the return type matching the variable's type. 3. Use a return statement to return the variable's value.
What are the general steps to write a toString() method?
1. Use the @Override annotation. 2. Construct a string representation of the object's state using string concatenation. 3. Return the constructed string.
What is the process when System.out.println(object) is called?
1. The object's toString() method is automatically called. 2. The toString() method returns a string. 3. The string is printed to the console.
What are the steps to access an object's data using a getter method from another class?
1. Create an instance of the class containing the getter method. 2. Call the getter method on the object using the dot operator. 3. Store or use the returned value.
What happens when a getter method is called?
1. The program executes the code within the getter method. 2. The getter method returns a copy of the value of the instance variable. 3. The program continues execution from where the getter method was called.
What happens when the `return` keyword is encountered in a method?
1. The method's execution is immediately terminated. 2. The specified value is returned to the calling method. 3. Execution resumes in the calling method at the point immediately following the method call.
What is the process of creating a new object using a constructor?
1. Allocate memory for the new object. 2. Execute the constructor code, initializing instance variables. 3. Return a reference to the newly created object.
What are the steps to call a method on an object?
1. Use the object's reference followed by the dot operator. 2. Specify the method name and any required arguments within parentheses. 3. The method is executed using the object's data.
What are the steps when a program calls a method?
1. The program jumps to the beginning of the called method. 2. The code inside the method is executed. 3. Once the method finishes, the program returns to the point where the method was called.
What is the process of accessing a private variable from within the same class?
1. The private variable is directly accessed by its name within a method of the class. 2. No special syntax is required, as access is implicitly granted within the class scope.