Interfaces

  • Interface is a keyword which tells what a class must do but not how to do means lack of instance variables and no definition for the methods.
  • Once interface is defined, any number of classes can implement interface and also one class can implement any number of interfaces.
  • By using interface Java allows us "one interface , multiple methods" of polymorphism as the classes which implements the interface should provide the definition for all the methods in interfaces.
  • Interfaces are designed to support dynamic method resolution at run time Since interfaces are in a different hierarchy from classes, it is possible for classes that are unrelated in terms of the class hierarchy to implement the same interface.

Defining an Interface

General form of 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;

}

default- when there is no specified access modifier then it is default which can be accessed by the other members in the same package.

public- If we uses public then it will access to all members then it the source file should be saved with the name of the interface.

Instance variables- The variables in the interface are implicitly final and static where we can not modify the value of the variable.

***In JDK 8, it is possible to add a default implementation to an interface method. Thus, it is now possible for interface to specify some behavior.

Example

interface Callback 
{
 void callback(int i);
}

Implementing Interfaces

To implement the interface we have to use implements keyword in the part of the class definition which provides the definition for all methods as follows

General form is::

class classname [extends superclass] [implements interface [,interface...]]

{

// class-body

}

  • If the class implements more than one interface those should be separate by comma, If both interfaces have same method then the same method will be of either classes.
  • The methods that implements from interfaces should be public
//simple example for interfaces
interface Callback 
{
 void callback(int i);
}
class Client implements Callback 
{
 // Implement Callback's interface should be public
 public void callback(int p)  
 {
 System.out.println("callback called with " + p);
 }
 //class may have it's own methods also
 void nonIfaceMeth()
 {
 System.out.println("Classes that implement interfaces " + "may also define other members, too.");
 }
}
public class InterfaceDemo
{
public static void main(String args[])
{
  Client ob = new Client();
  ob.callback(23);
}
}.

Output:

callback called with 23

Classes that implement interfaces may also define other members, too.

Accessing Implementations Through Interface References

we can declare variables as object references that use an interface rather than a class type.

When we call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to.

This is similar as reference of super class to a sub class in inheritance.

//example to using interface references to access interface methods.
interface Callback
{
void callback(int i);
}

class Client implements Callback
{
// Implement Callback's interface
public void callback(int p) 
{
System.out.println("callback called with " + p);
}
 void nonIfaceMeth()
 {
 System.out.println("Classes that implement interfaces " + "may also define other members, too.");
 }
}

public class TestIface 
{
 public static void main(String args[]) 
 {
 Callback c = new Client();
 c.callback(42);
 //c.nonIfaceMeth(); it will show error as c is the reference of Callback interface yet it is instance of class Client we can not call the Client method. 
 }
}

Output:

callback called with 42

//example to show the power of polymorphism
interface Callback
{
void callback(int i);
}

class Client implements Callback
{
// Implement Callback's interface
public void callback(int p) 
{
System.out.println("callback called with " + p);
}
 void nonIfaceMeth()
 {
 System.out.println("Classes that implement interfaces " + "may also define other members, too.");
 }
}

// Another implementation of Callback.
class AnotherClient implements Callback
{
 // Implement Callback's interface
 public void callback(int p)
 {
 System.out.println("Another version of callback");
 System.out.println("The value of p is::"+p);
 }
}

public class TestIface 
{
 public static void main(String args[]) 
 {
 Callback c = new Client();
 AnotherClient ob = new AnotherClient();
 c.callback(44);
 c = ob; // c now refers to AnotherClient object
 c.callback(99);
 }
}

Output:

callback called with 44
Another version of callback
The value of p is::99

Partial Implementations

  • If the class implements the interface but not fully implement the methods in the interface then such class should be declared as abstract.
  • The classes that extends this abstract class should be either implements the interface method or it also declared as abstract.
//example for partial implementations
interface Callback
{
void callback(int i);
}

abstract class Incomplete implements Callback 
{
 int a, b;
  void show() 
  {
 System.out.println(a + " " + b);
 }
 //...
}

class Client extends Incomplete
{
  public void callback(int p)
  {
    System.out.println("The value of p is::"+p);
  }
}
public class TestIface 
{
 public static void main(String args[]) 
 {
 Callback c = new Client();        //reference of interface and instance of class Client
  Client client = new Client();    // object of Client class

   c.callback(44);

   client.a =10;
   client.b =20;
   client.show();

   client.callback(55);
 }
}

Output:

The value of p is::44
10 20
The value of p is::55

Nested Interfaces

  • An interface can be declared a member of a class or another interface. Such an interface is called a member interface or a nested interface.
  • A nested interface can be declared as public, private, or protected. This depends on top-level interface, which must either be declared as public or use the default access level.
  • 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.
// A nested interface example.
// This class contains a member interface.
class A 
{
 // this is a nested interface
 public interface NestedIF 
 {
 boolean isNotNegative(int x);
 }
}
// B implements the nested interface.
class B implements A.NestedIF //with the fukky qualified name of enclosed class of nested class
{
 public boolean isNotNegative(int x) 
 {
 return x < 0 ? false: true;
 }
}

public class NestedIFDemo 
{
 public static void main(String args[]) 
 {
 // use a nested interface reference
 A.NestedIF nif = new B();


 System.out.println("10 is not negative::"+nif.isNotNegative(10));


 System.out.println("-12 is not negative::"+nif.isNotNegative(-12));
 }
}

Output:

10 is not negative::true
-12 is not negative::false

Applying Interfaces

As we know the stack operations push() and pop() in which the operation of push() pop() may differs when we used fixed size stack and growable size stack but the methods to be performed by the stack are same.

//example of applying interfaces
//example of applying interfaces

// Define an integer stack interface.
interface IntStack 
{
 void push(int item); // store an item
 int pop(); // retrieve an item
}

// An implementation of IntStack that uses fixed storage.
class FixedStack implements IntStack 
{
 private int stck[];
 private int tos;

  // allocate and initialize stack
 FixedStack(int size) 
 {
 stck = new int[size];
 tos = -1;
 }
 // Push an item onto the stack
 public void push(int item)
 {
 if(tos==stck.length-1) // use length member
 System.out.println("Stack is full.");
 else
 stck[++tos] = item;
 }
  // Pop an item from the stack
 public int pop() 
 {
 if(tos < 0) {
 System.out.println("Stack underflow.");
 return 0;
 }
 else
 return stck[tos--];
 }
}

// Implement a "growable" stack.
class DynStack implements IntStack 
{
 private int stck[];
 private int tos;

 // allocate and initialize stack
 DynStack(int size) 
 {
 stck = new int[size];
 tos = -1;
 }

  // Push an item onto the stack
 public void push(int item) 
 {
 // if stack is full, allocate a larger stack
 if(tos==stck.length-1)
 {
 int temp[] = new int[stck.length * 2]; // double size
 for(int i=0; i<stck.length; i++) 
   temp[i] = stck[i];

   stck = temp;
 stck[++tos] = item;
 }
 else
 stck[++tos] = item;
 }

  // Pop an item from the stack
 public int pop() {
 if(tos < 0) {
 System.out.println("Stack underflow.");
 return 0;
 }
 else
 return stck[tos--];
 }
}

public class StackDemo
{
public static void main(String args[])
{
  IntStack ob = new FixedStack(5);
  DynStack dy = new DynStack(2);

  for(int i=0;i<5;i++)
    ob.push(i+1);
  System.out.println("elements in fixed size stack are::");

  for(int j=0;j<5;j++)
    System.out.print(ob.pop()+" ");

  System.out.println();

  ob=dy;

  for(int i=0;i<6;i++)
    ob.push(i+1);

 System.out.println("elements in dynamic length stack are::");
  for(int j=0;j<6;j++)
    System.out.print(ob.pop()+" ");

}
}

Output:

elements in fixed size stack are::
5 4 3 2 1
elements in dynamic length stack are::
6 5 4 3 2 1

Variables in Interfaces

  • We can use the interfaces shared constants in to multiple classes by implementing the interface which declared as final and initialized with a value.
  • If there is no methods to implement from interfaces then we need not to implement in the classes simply we can use its variables as constants.
 //example for using variables in interfaces
  interface SharedConstants 
{
 int NO = 0;
 int YES = 1;
 int MAYBE = 2;
 int LATER = 3;
 int SOON = 4;
 int NEVER = 5;
}

public class ExampleForUsingVariables implements SharedConstants
{
  public static void call(int i)
  {
   switch(i) 
   {
 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[])
  {
    call(1);
    call(3);
    call(5);
    call(0);

  }
}

Output:

Yes
Later
Never
No

Extending Interfaces

  • We can extends Interface by using extends keywords where the defining the extension is same as extending the class in inheritance.
  • If a class implements the interface which is extends from another then that class should implement the all the methods depends on the chain of inheritance.
// One interface can extend another.
interface A 
{
 void meth1();
 void meth2();
}

// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A 
{
 void meth3();
}


// This class must implement all of A and B
class MyClass implements B 
{
 public void meth1() 
 {
 System.out.println("Implement meth1().");
 }
 public void meth2() 
 {
 System.out.println("Implement meth2().");
 }
 public void meth3() 
 {
 System.out.println("Implement meth3().");
 }
}

public class IFExtend 
{
 public static void main(String arg[]) 
 {
 MyClass ob = new MyClass();
   ob.meth1();
 ob.meth2();
 ob.meth3();
 }
}

Output:

Implement meth1().

Implement meth2().

Implement meth3().

results matching ""

    No results matching ""