Java Interface keyword

Java Interface keyword

Interface Keyword In Java
The interface keyword takes the concept of abstractness one step further
The abstract keyword allows you to create one or more undefined methods in a class—you provide part of the interface without providing a corresponding implementation.
The implementation is provided by inheritors.
The interface keyword produces a completely abstract class, one that provides no implementation at all.
It allows the creator to determine method names, argument lists, and return types, but no method bodies.
An interface provides only a form, but no implementation.
An interface says, "All classes that implement this particular interface will look like this."
Thus, any code that uses a particular interface knows what methods might be called for that interface, and that’s all.
So the interface is used to establish a "protocol" between classes. (Some object-oriented programming languages have a keyword called protocol to do the same thing.) 
To create an interface, use the interface keyword instead of the class keyword. As with a class, you can add the public keyword before the interface keyword (but only if that interface is defined in a file of the same name).
If you leave off the public keyword, you get package access, so the interface is only usable within the same package.
An interface can also contain fields, but these are implicitly static and final.
To make a class that conforms to a particular interface (or group of interfaces), use the implements keyword, which says, "The interface is what it looks like, but now I’m going to say how it works." Other than that, it looks like an inheritance. 
The diagram for the instrument example shows this - 
Interface is a sort skeleton of classes containing method with signature, but no implementation.
All interface methods are by default public, and public abstract, and all interface variables are by default public, static, and final.
Interface is an keyword used to declare interface, and implement keyword is used to implement an interface in a class.
The interface is an alternative way to achieve multiple inheritance in java .
  • Using the keyword interface, you can fully abstract a class interface from its implementation.
  • That is, using the interface, you can specify what a class must do, but not how it does it. Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without anybody. In practice, this means that you can define interfaces that don’t make assumptions about how they are implemented. Once it is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces.
  • Class must define all interface methods with public access modifier that must be remembered.
Defining an Interface
access interface name {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
For a top-level interface, access is either public or not used. 
Suppose When no access modifier is included, then default access results, and the interface is available only to other members of its package.
When it is declared as public, the interface can be used by any other code. (When an interface is declared public, it must be in a file of the same name.) name is the name of the interface and can be any valid identifier.
We will see a small example of interface definition, in the example, we can see a function named rolled(), and which takes two integer parameters.
Example -
interface rolled{
void rolled(int a, int b);
Syntax to implement Interface In Java

class classname [extends superclass] [implements interface [,interface...]] {
// class-body

Here is a small example class that implements the Rolled interface shown earlier.
class Client implements Rolled{
// Implement Callback's interface
public void rolled(int p, int m) {
System.out.println("callback called with " + p);
Example - Example uses this technique to implement an automated decision-maker- 
import java.util.Random;
interface SharedConstants {
int NO = 0;
int YES = 1;
int MAYBE = 2;
int LATER = 3;
int SOON = 4;
int NEVER = 5;
class Question implements SharedConstants {
Random rand = new Random();
int ask() {
int prob = (int) (100 * rand.nextDouble());
if (prob < 30)
return NO; // 30%
else if (prob < 60)
return YES; // 30%
else if (prob < 75)
return LATER; // 15%
else if (prob < 98)
return SOON; // 13%
return NEVER; // 2%
class AskMe implements SharedConstants {
static void answer(int result) {
switch(result) {
case NO:
case YES:
case MAYBE:
case LATER:
case SOON:
case NEVER:

public static void main(String args[]) {
Question q = new Question();

Output - Here is the output of a sample run of this program. Note that the results are different each time it is run.

Multiple Interface -
Multiple interface is nothing but a way of implementing more than one interface, To implement multiple interface separate them with comma.
Example -
interface InterfaceNOOne{
  public void sample(); // interface method

interface InterfaceNOTwo {
  public void sample2(); // interface method

class A implements InterfaceNOOne, InterfaceNOTwo {
  public void sample() {
    System.out.println("Web designing House");
  public void sample2() {
    System.out.println("Online Promotion House");

class Main {
  public static void main(String[] args) {
    A ob= new S();