Explain Interface?Define
By Ramakrishna on Dec 16, 2008 in Uncategorized
Interface:
In Java language, an interface is like a stripped-down class; it specifies a set of methods that an instance must handle, but it omits inheritance relations and method implementations.
Object-oriented programming is sometimes modeled as communication between objects like one object talks to, or sends a message to, another object ( by invoking a method on it ). An object needs to know the order to talk with another object ; a set of abstract methods i.e., method minus implementation information. Put the another way, an interface specifies how we can talk to an object,but says nothing about what kind of object will handle our messages. It is job of one or more class instances to provide the substance behind an interface’s promise.
The general form of an interface definition is :
interface name
{
variable declarations;
method declarations;
}
Here, interface is the keyword and name is any valid Java variable ( just like class names ).
Variables are declared as follows:
public static final type variableName = value;
Methods declaration will contain only a list of methods without any body statements.
Example:
return-type methodName(parameter_list);
The following is an example of an interface definition that contains two variables and one method :
interface Item
{
public static final int prodCode = 1001;
public static final String prodName = “Fan”;
public abstract void display();
}
Here the code for the method is not included in the interface and the method declaration simply ends with a semicolon. The class that implements this interface must define the code for the method.
NOTE:
–> All the Interface methods are Abstract only by Default.
–> we can’t create an object to the interface. But we can create a reference variable to interface.
–> Once the interface is provided we can create ‘n’ number of implementation classes
–> we can create object to implementation class by using this we can allow all features of the super class.
