javascript
Brief description  about Online courses   join in Online courses
OR

Interfaces

Tanya  Sharma
Tanya Sharma
Java Team Lead

An abstract class mixes the idea of mutable data in the form of instance variables, non-abstract
methods, and abstract methods. An abstract class with only static final instance variables and all
abstract methods is called an interface. An interface is a specification, or contract, for a set of
methods that a class that implements the interface must conform to in terms of the type signature of
the methods. The class that implements the interface provides an implementation for each method,
just as with an abstract method in an abstract class.
So, you can think of an interface as an abstract class with all abstract methods. The interface itself
can have either public, package, private or protected access defined. All methods declared in an
interface are implicitly abstract and implicitly public. It is not necessary, and in fact considered
redundant to declare a method in an interface to be abstract.
You can define data in an interface, but it is less common to do so. If there are data fields defined in
an interface, then they are implicitly defined to be:
• public.
• static, and
• final
In other words, any data defined in an interface are treated as public constants.

Interface declaration

Interface names and class names in the same package must be distinct.
public interface interface-name {
// if any data are defined, they must be constants
public static final type-name var-name = constant-expr;
// one or more implicitly abstract and public methods
return-type method-name ( formal-params );
}
An interface declaraion is nothing more than a specification to which some class that purports to
implement the interface must conform to in its implementation. That is, a class the implements the
interface must have defined implementations for each of the interface methods.
The major reason for interfaces being distinct elements in Java is that you often want to define some
operation to operate on objects that all conform to the same interface. So, you can define some code in
a very general way, with a guarantee that by only using the methods defined in the interface, that all
objects that implement the interface will have defined implementations for all the methods.
For example, you might define a Shape interface that defines an area() method, and so you would
expect that any class that implements the Shape interface, would define an area method. So, if I
have a list of references to objects that implement the Shape interface, I can legitimately invoke the
area method, abstractly, on each of these objects and expect to obtain as a result a value that
represents the area of some shape.

Separation of Interface from Implementations

Interfaces are specifications for many possible implementations. Interfaces are used to define a
contract for how you interact with an object, independent of the underlying implementation. The
objective of an object-oriented programmer is to separate the specification of the interface from the
hidden details of the implementation.
Consider the specification of a common LIFO stack.
public interface StackInterface {
boolean empty();
void push(Object x);
Object pop() throws EmptyStackException;
Object peek() throws EmptyStackException;
}

Write your comment now