Java Interface

Java Interface


Interface In Java
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 a 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 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%
else
return NEVER; // 2%
}
}
class AskMe implements SharedConstants {
static void answer(int result) {
switch(result) {
case NO:
System.out.println("No");
break;
case YES:
System.out.println("Yes");
break;
case MAYBE:
System.out.println("Maybe");
break;
case LATER:
System.out.println("Later");
break;
case SOON:
System.out.println("Soon");
break;
case NEVER:
System.out.println("Never");
break;
}
}

public static void main(String args[]) {
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
}
}
Output - Here is the output of a sample run of this program. Note that the results are different each time it is run.
Later
Soon
No
Yes

 
Multiple Interface -
Multiple interface is nothing but way of implementing more then one interface, To implement multiple interface separate them with coma.
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();
    ob.sample();
    ob.sample2();
  }
}