Powered by Blogger.

Saturday 26 March 2016

PACKAGE

No comments :
To create a package is quite easy: simply include a package command as the first statement in java source file. Any classes declared within that file will belong to the specified package. The package statement defines a name space in which classes are stored.If you omit the package statement, the class names are put into the default package. This is the general form of the package statement:         
                                                                    package pkg;
Here, pkg is the package name of the package. You can create a hierarchy of packages. To do so, simply separate each package name from the one above it by use of a period.The general form of a multileveled package.
                                                            package pkg1.pkg2.pkg3;
A package hierarchy must be reflected in the file system of your java Development system.

A Short Package Example:

Access Protection

No comments :
The three access modifiers, private, public, and protected provide a variety of way to Process the many levels of access required by these categories. while java's access control mechanism may seem complicated, we can simplify it as follow. Anything declared public can be accessed from anywhere. Anything declared private can not be seen outside of its class. when a member does not have an explicit access specification, it is visible to subclasses as well as to other class in the same package. this is the default access. if you want to allow an element to be seen outside your current package, but only to classes that subclass your class directly, then declare that element protected.
Class Member Access Table:
Private No Modifier Protected Public
Same class Yes Yes Yes Yes
Same package subclass No Yes Yes Yes
Same package non-subclass No Yes Yes Yes
Different package subclass No No Yes Yes
Different package non-subclass No No No Yes

Here, Table applies only to members of classes. A non-nested class has only two possible access levels: default and public. when a class is declared as public, it is accessible by any other code. if a class has default access, then it can only be accessed by other code within its same package. when a class is public, it must be the only public class declared in the file, and the must have the same name as the class.

An Access Example: It has two package and five classes. Remember that the class for the two different packages need be stored in directories named after there respective package- in this case, p1 and p2
The source for the first package define three package: Protection, Derived and samePackage. The first class defines four int variables in each of the legal protection modes. The n is declared with the default protection, n_pri is private, n_pro is protected and n_pub is public.
The second class, Derived, is a subclass of Protection in the same package, p1. This grants Derived access to every variable in Protection except for n_pri, the private one. The third class, samePackage, is not a subclass of Protection, but is in the same package and also has access to all but n_pri.
This is file Protection.java

Following is the source code for the other package,p2. The two classes defined in p2 cover the other two conditions that are affected by access control. The first class, protection2, is a subclass of p1.Protection. This grants access to all of p1.protection's variables except for n_pir (because it is private) and n, The variable declared with the default protection. Remember, the default only allows within the class or the package, not extra-package subclass. Finally, the class otherPackage has access to only one variable, n_pub, which was declared public.
This is file protection2.java

If you want to try these two packages, here are two test files (runp1package and runp2package) you can use. The one package p1 is show here:

The one package p2 is show here:

Interfaces

No comments :
Interfaces are designed to support dynamic method resolution at run time. Normally, in order for a method to be called from one class to another, both classes need to be present at compile time so the java compile can check to insure that the method signatures are compatible. This requirement by itself makes for a static and non-extensible classing environment.Inevitable in a system like this, functionality gets pushed up higher and higher in the class hierarchy so that the mechanism will be available to more and more subclasses. Interfaces are designed to avoid this problem. They disconnect the definition of a method or set of methods from the inheritance hierarchy.
Defining an Interface:
An interface is defined much like a class. This is a simplified general form of an interface:

Implementing Interfaces:
Once an interface has been defined, one or more classes can implemented that interfaces. To implemented an interface, include the implements clause in an class definition, and then create the method required by the interface. The general form of the class that include implementations clause look like this:

If a class implements more than one interface, the interfaces are separated with a comma. if a class implements two interfaces that declared same method, then the same method will be used by clients of either interface. The methods that implement an interface must be declared public. Also, the type signature of the implementing method must match exactly the type of signature in the interface defined.
Here, is a small Example class that implements the callback interface shown earlier:

Partial Implementations:
If a class includes an interface but does not fully implement the methods required by that interface, then that class must be declared as abstract.

Here, the class Incomplete does not implement callback() and must be declared as abstract. Any class that inherits Incomplete must implement callback() or be Declared abstract itself.
Nested Interface:
An interface can be declared a member of a class or another interface. such an interface is call a member interface or nested interfaced. A nested interface can be declared as public, private and protected. This differs from a top-level interface, which must either be declared as public or use the default access level, as previously described. when a nested interface is used outside of its enclosing scope, it must be qualified by the name of the class or interface of which it is a member.br/> Here, is an Example:

Applying Interfaces:
You Developed a class called Stack that implemented a simple fixed-size stack. However, there are many ways to implement a stack. for Example, the stack can be of a fixed size or it can be "growable" the stack can also be held in an array, a linked list, a binary tree, and so on. No matter how the stack is implemented, the interface to the stack remains the same. That is , the method push() and pop() define the interface to a stack independently of the details of the implementations. because the interface to a stack is separated from its implementation, it is easy to define a stack interface, leaving it to each implementation to define the specifics.
Example: