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.

Flip to see [answer/question]
Flip to see [answer/question]

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 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.

How are getter methods used in data validation?

Getter methods can be used to retrieve data for validation purposes before further processing or storage.

How is the toString() method used in debugging?

It provides a human-readable representation of an object's state, aiding in identifying errors and understanding program behavior.

How are accessor methods used in API design?

They provide a controlled interface for accessing data in a class, promoting modularity and maintainability.

How are accessor methods used in database interactions?

Accessor methods can be used to retrieve data from objects that represent database records.

How are accessor methods used in GUI applications?

They are used to access and display data in UI elements.

How is toString() used in logging?

It helps to create meaningful log messages by providing a string representation of objects.

In what scenarios are accessor methods particularly useful?

When controlled access to object data is required, such as when validating inputs or protecting internal state.

How are accessor methods used in implementing the Model-View-Controller (MVC) design pattern?

They are used by the View to retrieve data from the Model for display.

How are accessor methods used in unit testing?

They are used to inspect the state of an object after a method has been called, allowing for verification of expected outcomes.

How are accessor methods used in data serialization?

They are used to retrieve the values of an object's attributes, which are then serialized into a different format (e.g., JSON, XML) for storage or transmission.