Final Classes Methods


Controlling Horizontal & Vertical Access

The term horizontal access is used to describe the level of access an object of one type has to the members (i.e., fields and methods) of another type. This topic was discussed in detail in chapter 9. The term vertical access refers to the level of access a derived class has to its base class members. In both cases access is controlled by the three Java access modifiers public, protected, and private. There is a fourth level of access, known as package, that is inferred by the omission of an access modifier. In other words, if you declare a class member, be it a field or method, and don’t specify it as being either public, protected, or private, then it is, by default, package. (The exception to this rule is when you are declaring an interface in which case the members are public by default.)

The behavior of protected and package accessibility is dictated by what package the classes in question belong. Figure 11-18 gives a diagram showing five classes named ClassOne, ClassTwo, ClassThree, ClassFour, and ClassFive. ClassOne, ClassTwo, and ClassThree belong to Package A, and ClassFour and ClassFive belong to Package B.

image from book
Figure 11-18: Horizontal And Vertical Access In Multi-Package Environment

Referring to figure 11-18 — access behavior is discussed relative to ClassOne from both the horizontal and vertical access perspective. ClassOne contains one package member, one public member, one protected member, and one private member.

ClassTwo inherits from ClassOne and belongs to the same package. From a vertical perspective, as a subclass of ClassOne it has access to ClassOne’s public, protected, and package members. From a horizontal perspective it can also access ClassOne’s public, protected, and package members.

ClassThree is a standalone class in the same package as ClassOne. Since it is not a subclass of ClassOne it has no vertical access. Horizontally it can access ClassOne’s public, protected, and package members.

ClassFour is a standalone class in a different package than ClassOne. It too is not a subclass of ClassOne and therefore has no vertical access. Horizontally it can only access ClassOne’s public members.

ClassFive is a subclass of ClassOne but belongs to another package. It has vertical access to ClassOne’s public and protected members. It has horizontal access to ClassOne’s public members.

The following examples give the code for each class shown in figure 11-18 with offending lines commented out.

Example 11.15: ClassOne.java

image from book
 1     package packageA; 2 3     public class ClassOne { 4       void  methodOne(){} 5       public void methodTwo(){} 6       protected void methodThree(){} 7       private void methodFour(){} 8     }
image from book

Example 11.16: ClassTwo.java

image from book
 1     package packageA; 2 3     public class ClassTwo extends packageA.ClassOne { 4       packageA.ClassOne c1 = new packageA.ClassOne(); 5 6       void f(){ 7           c1.methodOne(); 8           c1.methodTwo(); 9           c1.methodThree(); 10       // c1.MethodFour(); // Error - no horizontal access to private member 11          methodOne(); 12          methodTwo(); 13          methodThree(); 14       // methodFour();    // Error - no vertical access to private member 15      } 16    }
image from book

Example 11.17: ClassThree.java

image from book
 1     package packageA; 2 3     public class ClassThree { 4       packageA.ClassOne c1 = new packageA.ClassOne(); 5 6       void f(){ 7           c1.methodOne(); 8           c1.methodTwo(); 9           c1.methodThree(); 10       // c1.methodFour();    // Error - no horizontal access to private member 11 12      } 13    }
image from book

Example 11.18: ClassFour.java

image from book
 1     package packageB; 2 3     public class ClassFour { 4        packageA.ClassOne c1 = new packageA.ClassOne(); 5 6        void f(){ 7         // c1.methodOne();    // Error - no horizontal access to package member in different package 8            c1.methodTwo(); 9         // c1.methodThree(); // Error - no horizontal access to protected member 10        // c1.methodFour(); // Error - no horizontal access to private member 11       } 12    }
image from book

Example 11.19: ClassFive.java

image from book
 1     package packageB; 2 3     public class ClassFive extends packageA.ClassOne { 4        packageA.ClassOne c1 = new packageA.ClassOne(); 5 6       void f(){ 7         // c1.methodOne();    // Error - no horizontal access to outside package member 8            c1.methodTwo(); 9         // c1.methodThree();  // Error - no horizontal access to protected member outside package 10        // c1.MethodFour();  // Error - no horizontal access to private member 11        // methodOne();       // Error - no vertical access outside package 12           methodTwo(); 13           methodThree(); 14        // methodFour();     // Error - no vertical access - private method 15       } 16    }
image from book

To test each class simply compile each one. Experiment by removing the comments from an offending line and then compiling and studying the resulting error message(s).

Quick Review

Horizontal and vertical access is controlled via the access specifiers public, protected, and private. A class member without an explicit access modifier declared has package accessibility by default. Essentially, classes belonging to the same package can access each other’s public, protected, and package members horizontally. If a subclass belongs to the same package as its base class it has vertical access to its public, protected, and package members.

Classes belonging to different packages have horizontal access to each other’s public members. A subclass in one package whose base class belongs to another package has horizontal access to the base class’s public methods only but vertical access to both its public and protected members.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

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