Game Development Reference
In-Depth Information
ors in a superclass to be accessed only by subclasses of that superclass in other pack-
ages (such as the invincibagel package) or by any class within the same package as the
class containing those protected members (Java constructs).
This access modifier keyword essentially protects methods and variables in a class
that is intended to be (hoped to be used as) a superclass by being subclassed ( exten-
ded ) by other developers. Unless you own the package that contains these protected
Java constructs (which you do not), you must extend the superclass and create your
own subclass from that superclass to be able to use the protected methods.
You may be wondering, why would one want to do this, protecting Java code struc-
tures in this way? When you are designing a large project, such as the Android OS API,
you will often want to have the higher-level methods and variables not be used directly,
right out of, or from within, that class, but rather within a more well-defined subclass
structure.
You can achieve this direct usage prevention by protecting these methods and vari-
able constructs from being used directly such that they become only a blueprint for
more detailed implementations in other classes and are not able to be used directly. Es-
sentially, protecting a method or variable turns it into a blueprint or a definition only.
Java'sPrivateModifier:Variables,Methods,andClasses
Get Local Access Only
The Java private access control modifier keyword can be used by data fields (variables
or constants) and by methods, including constructor methods, but cannot be used by
classes or interfaces. The private modifier can be used by a nested class; however, it
cannot be used by an outer or the primary (topmost) class. The private access control
keyword allows variables, methods, and constructors in a class to be accessed only in-
side that class. The private access control keyword allows Java to implement a concept
called encapsulation, in which a class (and objects created using that class) can encap-
sulate itself, hiding its “internals” from outside Java universe, so to speak. The OOP
concept of encapsulation can be used in large projects to allow teams to create (and,
more importantly, debug) their own classes and objects. In this way, no one else's Java
code can break the code that exists inside these classes, because their methods, vari-
ables, constants, and constructors are private!
The access modifier keyword essentially privatizes methods or variables in a class
so that they can only be used locally within that class or by objects created by that
class's constructor method. Unless you own the class that contains these private Java
constructs, you cannot access or use these methods or data fields. This is the most re-
Search WWH ::




Custom Search