9) Discuss the public and private modifiers in context of methods and instance variables.

What will be an ideal response?


The modifiers public and private can both be used with methods and instance variables. Any instance variable can be labeled either public or private. The modifier public means that there are no restrictions on where the instance variable can be used. The modifier private means that the instance variable cannot be accessed by name outside of the class definition.
The modifiers public and private before a method definition have a similar meaning. If the method is labeled public, there are no restrictions on its usage. If the method is labeled private, the method can only be used in the definition of another method of the same class.
Normal good programming practices require that all instance variables be private and typically most methods be public.

Computer Science & Information Technology

You might also like to view...

Which of the following macro security settings is the default setting in Word?

A) Disable all macros with notification B) Trust access to the VBA project object model C) Disable all macros without notification D) Disable all macros except digitally signed macros

Computer Science & Information Technology

Which of the following methods correctly withdraws an amount from a bank account?

A. Method withdraw(Numeric amt)    acctBal = acctBal - amt    Display "The new balance is: $" + acctBal End Method B. Method withdraw(String amt)    acctBal = acctBal - amt    Display "The new balance is: $" + acctBal End Method C. Method withdraw(Numeric amount)    acctBal = acctBal - amt    Display "The new balance is: $" + acctBal End Method D. Method withdraw(String amount)    acctBal = acctBal - amt    Display "The new balance is: $" + acctBal End Method

Computer Science & Information Technology

The values used to initialize a structure are enclosed in braces and separated by commas.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

_________ methods can be called without the need for an object of the class to exist.

a) special b) independent c) static d) dependent

Computer Science & Information Technology