java:access_specifiers_accessors_and_mutators
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:access_specifiers_accessors_and_mutators [2020/09/12 01:30] – [Access specifiers] mithat | java:access_specifiers_accessors_and_mutators [2020/09/12 02:01] (current) – [this] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
===== Access modifiers ===== | ===== Access modifiers ===== | ||
Line 19: | Line 19: | ||
</ | </ | ||
- | But the definition has issues. For example, if we change our example | + | But the definition has issues. For example, if we change our '' |
<file java ClickerExample.java> | <file java ClickerExample.java> | ||
Line 37: | Line 37: | ||
</ | </ | ||
- | the object will happily oblige the user's wish to set the count to some arbitrary value. That's not something you can typically do with a clicker counter and probably not what you want out from your '' | + | the object will happily oblige the user's wish to set the count to some arbitrary value. That's not something you can typically do with a clicker counter and probably not what you want from your '' |
- | This is easy to solve with Java' | + | We can add protection of members using **access modifiers**. Let's look at a modified version of our class definition that gives us some protection |
<file java ClickerCounter.java> | <file java ClickerCounter.java> | ||
Line 56: | Line 56: | ||
</ | </ | ||
- | Access modifiers are placed before the start of a member. Java offers four levels of protection, and they just so happen to all start with the letter ' | + | Access modifiers are placed before the start of a member |
* '' | * '' | ||
* '' | * '' | ||
Line 62: | Line 62: | ||
* '' | * '' | ||
- | With the changes made above, if we try to run '' | + | If we try to compile |
<code java> | <code java> | ||
Line 68: | Line 68: | ||
</ | </ | ||
- | it cannot access the '' | + | it cannot access the '' |
<WRAP center round tip 80%> | <WRAP center round tip 80%> | ||
- | In Java programming, | + | It's considered a best practice |
- | + | ||
- | Methods can be at whatever access level is appropriate for their use. If it is part of the class' interface, it should be '' | + | |
</ | </ | ||
+ | |||
+ | We want objects to be **self-governing**, | ||
+ | |||
+ | |||
Line 233: | Line 235: | ||
===== this ===== | ===== this ===== | ||
- | FIXME | + | Every class has an automatically generated '' |
+ | |||
+ | '' | ||
+ | |||
+ | |||
+ | <code java> | ||
+ | public void setMaxCount(int maxCount) { | ||
+ | if (maxCount > 0) { | ||
+ | this.maxCount = maxCount; | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | We have changed the name of the parameter to '' | ||
+ | '' | ||
- | Copyright © 2011-2018 | + | Copyright © 2020 Mithat Konar. All rights reserved. |
java/access_specifiers_accessors_and_mutators.1599874223.txt.gz · Last modified: 2020/09/12 01:30 by mithat