Modifiers - Default Value - Default Values for Attributes/Properties

3 important questions on Modifiers - Default Value - Default Values for Attributes/Properties

It is also possible to assign a default value to a property.

In Fig. 6.11, we modify the hasAdultPrivleges Boolean flag to have a default value of False.


What does it mean? 

This means that whenever we created a new Member instance, the flag will be given an initial value of False.

Give an example of why it might be useful to use the default value?

This helps us because whenever the Member tries to borrow an adult book, we would check the value of the flag.

If the field is False, we would then call the isAdult() operation.

If the Member is an adult, then we flip the flag, so that the next time the Member tries to borrow an adult book, we will see that the Member already has permission.

The birthday field is marked private. The value is available ..... Where?

Note that even though the birthdate field is marked private (-), the value is available inside to operations on Member.

Therefore, the isAdult() operation can read the birthdate field.

The question on the page originate from the summary of the following study material:

  • A unique study and practice tool
  • Never study anything twice again
  • Get the grades you hope for
  • 100% sure, 100% understanding
Remember faster, study better. Scientifically proven.
Trustpilot Logo