13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

88Chapter 3An Experienced Programmer’s Introducti<strong>on</strong> to Javaif they d<strong>on</strong>’t extend this class). Classes in other packages (unless they extendthis class) can not get at such members.A public data member or method may be accessed by any code inany class.What if you do not put an access specifier <strong>on</strong>? Then the item (data member,method, or class) has package visibility. Such an item is accessible to anyother class within the package, but no further. Not even derived classes, unlessthey are in the same package, are allowed to see it. 12In terms of how restrictive the access is, you can think of the terms in orderof decreasing strictness as:• private• (package) 13• protected• publicTIPBeginner Java programmers often declare everything as public, so that theycan ignore such issues. But then they get the OO religi<strong>on</strong>, and having experiencedreliability issues (others messing with their variables) they go to the otherextreme and declare private as much as possible. The problem here is thatthey often d<strong>on</strong>’t know how others will want to reuse their code. Restricting everythingto private makes reuse more narrow. We prefer using private fordata members but protected for those internal helper methods that you mightotherwise make private; this hides your implementati<strong>on</strong> from most otherclasses while allowing some<strong>on</strong>e to override your methods, effectively providinga way for them to override your implementati<strong>on</strong>. Allow those who would build<strong>on</strong> your work the ability to do so without having to reimplement.Here is a simple example of each type of declarati<strong>on</strong>:12. If you are a C++ programmer, the following descripti<strong>on</strong> may mean something to you(if not, skip this): All classes within a package are essentially “friends.”13. Remember there is no keyword for package level protecti<strong>on</strong>, rather it is the absence of akeyword that denotes this level of protecti<strong>on</strong>. We had to write something in that space <strong>on</strong> thepage so you’d know what we’re talking about.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!