Powered by Blogger.

Saturday 26 March 2016

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:

No comments :

Post a Comment

Please Write a Message for Programming or something Related issues.