1 Instance Variables Explain the purpose of an instance vari

1- (Instance Variables) Explain the purpose of an instance variable?

2- (set and get Methods) Explain why a class might provide a set method and a get method for

an instance variable?

Solution

1- Purpose of an instance variable :- A class provides an instance variable (or several instance variables) when each object of the class must maintain information separately from all other objects of the class.

For example, a class called Account that represents a bank account provides an instance variable to represent the balance of the account. Each Account object maintains its own balance but does not know the balances of the bank’s other accounts.

2- A class might provide a set method and a get method for an instance variable.

An instance variable is typically declared private in a class so that only the methods of the class in which the instance variable is declared can manipulate it.

This prevents the variable from being modified accidentally by a class in another part of the program.

In some cases, it may be necessary for an application to modify the private data. For example, the owner of a bank account should be able to deposit or withdraw funds and check the account’s balance.

A class’s designer can provide public set and get methods that enable an application to specify the value for, or retrieve the value of, a particular object’s private instance variable.

1- (Instance Variables) Explain the purpose of an instance variable? 2- (set and get Methods) Explain why a class might provide a set method and a get method fo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site