interface interface_name
{
return_type method1([arguments]);
return_type method2([arguments]);
ЕЕ...........................................
}
class class_name implements interface_name
{
ЕЕЕ............
body of the class
ЕЕЕ...........
}
An interface provides us a partial substitute for “multiple inheritance” in Java.
import java.lang.*;
interface MyStack
{
void Push(int x);
void Pop();
void Display();
}
class Stack implements MyStack
{
private int stck[];
private int tos;
Stack(int size)
{
stck=new int[size];
tos=-1;
}
public void Push(int x)
{
if (tos==stck.length-1)
{
System.out.println("Stack overflow ...");
return;
}
stck[++tos]=x;
}
public void Pop()
{
if (tos==-1)
{
System.out.println("Stack underflow ...");
return;
}
tos--;
}
public void Display()
{
System.out.println("Elements of the stack");
for(int i=tos;i>=0;i--)
System.out.println(stck[i]);
}
}
class App60
{
public static void main(String yck[])
{
Stack S=new Stack(3);
S.Push(10);
S.Push(20);
S.Push(30);
S.Display();
S.Push(40);
S.Pop();
S.Display();
}
}
output:
\>javac App60.java
\>java App60
Elements of the stack
30
20
10
Stack overflow ...
Elements of the stack
20
10
