To provide controlled access to an object's data, often instance variables.
Flip to see [answer/question]
Flip to see [answer/question]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Flip
Revise later
SpaceTo flip
If confident
All Flashcards
What is the purpose of accessor methods?
To provide controlled access to an object's data, often instance variables.
Why don't we create accessor methods for all instance variables?
To encapsulate data and maintain privacy for certain variables.
When is the toString() method automatically called?
When an object is printed directly using System.out.println().
What is the return type of a getter method?
The same type as the instance variable it returns.
Can private variables be accessed directly within the same class?
Yes, even though they are private, they can be accessed directly within the class's methods.
How does the 'return' keyword affect program flow?
It immediately exits the current method and returns control to the caller.
What is the significance of the @Override annotation?
It indicates that a method is intended to replace a method with the same signature in a superclass.
What is encapsulation?
The practice of hiding internal data and implementation details of an object from outside access, and only exposing a controlled interface.
Why is encapsulation important?
It promotes data integrity, reduces dependencies, and allows for easier modification of internal implementation without affecting external code.
What is the difference between returning a primitive type and returning an object reference?
Returning a primitive type returns a copy of the value. Returning an object reference returns a copy of the reference, not a copy of the object.
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?
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?
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?
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?
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?
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?
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?
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?
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?
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?
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.