Section 4.10. Visibility

   

4.10 Visibility

Visibility is similar to "scope" in ColdFusion and other languages such as Visual Basic. Everything in a program is not visible to every other thing all of the time. That is a prescription for chaos in code maintainability, encapsulation, and readability. In order to ensure the integrity of your data and the operations you perform on them, you declare classes, methods , and data as having certain visibility. You do that using access modifiers.

4.10.1 Access Modifiers

Access modifiers are composed of Java keywords that describe how something is stored or how it runs. Access modifiers are used in declaring variables (data fields ), methods, and classes. They determine what level of access the item will allow. That is, an access modifier determines where other methods or classes must be located to make use of this method or class. Possible values for access modifiers are private , protected , and public .

private: Any programmatic feature declared private will not be visible to other classes. This is the strictest level of access. Fields in a class should generally be declared private to ensure that they are only accessed or modified by methods defined to do so. A method that is private can only be called from other methods in the same class.

protected: A protected item restricts a method or class to use only by its subclasses.

public: Whatever is declared public is visible to the world; that is, the field or method or class can be called from any other class. Most methods are public, and the methods we will write in the next few chapters will generally be public.

Note

"Default" is not a possible value for an access modifier. It is not a keyword, but a concept for describing the access permissions when no access modifier is specified. By default, features have package-level visibility. We will look more at packages later, but essentially a package is a directory in which a class resides. To declare a feature with package visibility, you don't need to use any modifier.


There are three possible values for the type modifier: static , final , and abstract .

4.10.1.1 static METHODS

A static method is a method that does not operate on an object. It is associated with the class that defines it and does not need an object to do its work. For instance, the equals() method of the String class requires an object on which to operate ”making a comparison requires a thing to compare with another thing. On the other hand, consider the max() method defined in java.lang.Math . This method takes two numeric arguments and returns the greater value:

 public static int max(int a, int b) 

max() is declared static because it doesn't get called on a particular object; you can just call it when you need it.

static methods are useful for doing two things. The first, as mentioned, is when the method doesn't need access to the state of an object because you can supply the parameters explicitly, as with max() . The second reason to declare a method static is when it only needs access to the static fields of the class.

4.10.1.2 final METHODS

Methods are declared final in order to prevent an extended class from overriding the method. An important feature of OOP and Java is that the programmer can define certain functionality in a parent class, and then write classes that inherit its properties and extend its functionality. The programmer can override a method by defining it in a different way than the parent does. Declaring a method final prevents this.

The benefit to declaring a method final is that you can be certain of its implementation. You can declare classes, as well as methods, final . A class declared final cannot be extended at all, which limits its usefulness . On the other hand, you may not want people to have a large degree of flexibility when implementing your authorization classes, for example. One way to balance the need for security and extensibility is to leave the class open and mark the methods as final .

Note

The java.lang.String class is final . It can never be subclassed. If it could be subclassed, you could create your own String methods ”say, setCharAt() ”that could rewrite String objects. This would open a serious security hole, because you could, for instance, write an applet that gained access to otherwise restricted system files, thereby destroying the "sandbox" applets run in.


4.10.1.3 abstract METHODS

Abstract things lack concreteness. That is, you cannot create an object from an abstract class, just as you cannot find a concrete instance of something abstract in the world, such as the economy. There is no such thing as the economy; pretending like there is helps us organize things that are real, such as Alison's 50 pounds British sterling and Zoe's 100 shares of Macromedia stock. There is no "animal"; but there are individual lions, tigers , and bears. Abstract classes in Java operate under a similar principle. To translate the above reasoning into Java basically means that you cannot do this: new Economy() ;.

Any class with abstract methods must itself be declared abstract . This helps someone reading your code to quickly discover that the class is abstract without having to search all of the methods.

If this all seems a bit abstruse right now, that's okay. It is good to just know why you are writing all of those Java keywords when you make a new class or write a new method. We will look at all of this again when we discuss classes and objects.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net