java:access_specifiers_accessors_and_mutators
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
java:access_specifiers_accessors_and_mutators [2020/09/12 01:27] – created mithat | java:access_specifiers_accessors_and_mutators [2020/09/12 02:01] (current) – [this] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | ===== Access | + | ===== Access |
- | The '' | + | The '' |
+ | |||
+ | <file java ClickerCounter.java> | ||
+ | public class ClickerCounter { | ||
+ | |||
+ | int count; | ||
+ | |||
+ | void click(){ | ||
+ | count++; | ||
+ | } | ||
+ | |||
+ | void reset(){ | ||
+ | count = 0; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | But the definition has issues. For example, if we change our '' | ||
<file java ClickerExample.java> | <file java ClickerExample.java> | ||
Line 20: | 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. | + | 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 |
+ | |||
+ | 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 37: | 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 43: | Line 62: | ||
* '' | * '' | ||
- | With the changes made above, if we try to run '' | + | If we try to compile |
<code java> | <code java> | ||
Line 49: | 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 214: | 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.txt · Last modified: 2020/09/12 02:01 by mithat