Using Interface in Java with an Example

In this Java tutorial, we will learn about Interfaces. We will also learn using interface in java with the help of an example.

An interface is a blueprint of a class. Interfaces specify what a class must do but not how to do. An interface can have methods and variables just like a class. But, the methods declared in the interface must be abstract methods. Abstract method means a method without anybody. So, interface helps to achieve abstraction. Abstraction is the process of hiding the high-level background details from the user. It is an essential concept in Object Oriented Programming(OOP). We know that Java does not support multiple inheritance. But still, we can achieve multiple inheritance using interfaces. Any number of classes can implement an interface. Also, a class can implement many interfaces.

Defining an Interface in Java

An interface is declared by using “interface” keyword. A class that implements an interface must define all the methods declared in the interface. Interface fields are public, static and final by default, and the methods must be public and abstract. A class uses an interface by using “implements” keyword in java.

Simple Example of using Interface in Java

This example creates a simple interface ‘printing’. It has only one abstract method print. The class ‘Printer’ implements this interface by defining the print method.

interface printing{  
void print();  
}  
class Printer implements printing{  
public void print(){
System.out.println("Hello");
}  
public static void main(String args[]){  
Printer obj = new Printer();  
obj.print();  
}  
}

 

Implementing two types of Stack using Interface in Java

First, we declare an interface that defines an integer stack. Also, this interface will be used by both stack implementations.

interface IntStack {
void push(int item); 
int pop(); 
}

The following program creates a class called FixedStack that implements a fixed-length integer stack:

class FixedStack implements IntStack {
private int stack[];
private int tos;

FixedStack(int size) {
stack = new int[size];
tos = -1;
}

public void push(int item) {
if(tos==stack.length-1)
  System.out.println("Stack is full.");
else
  stack[++tos] = item;
}

public int pop() {
if(tos < 0) {
  System.out.println("Stack underflow.");
  return 0;
}
else
  return stack[tos--];
}

}

The following class creates a dynamic stack using IntStack interface:



class DynStack implements IntStack {
private int stck[];
private int tos;

DynStack(int size) {
stck = new int[size];
tos = -1;
}

public void push(int item) {

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;
}

public int pop() {
if(tos < 0) {
  System.out.println("Stack underflow.");
  return 0;
}
else
  return stck[tos--];
}
}

 

You can also read:

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *