Advanced Java Programming Lab
MS.R.SHANTHI PRABHA, M.Sc., M.Phil.,
Assistant professor
department of computer science
sacwc.
Introduction
Interfaces
Interfaces References
Sample Program
Example Program with Output
Introduction of Interface
An interface in java is a blueprint of a class
It has Static Constants and abstract methods.
The interface in java is a mechanism to archieve abstraction
There can be only absrtact methods in the java interface not
method body.
It cannot be instantiated like abstract class.
Sample Interface Program
interface MyInterface
{
/* All the methods are public abstract by default * As
you see they have no body
*/
public void method1();
public void method2();
}
Example Program
interface MyInterface
{
/* compiler will treat them as:
public abstract void method1();
* public abstract void method2();
*/ public void method1();
public void method2();
}
Con..
class Demo implements MyInterface
{
/* This class must have to implement both the abstract methods
else you will get compilation error
*/
public void method1()
{
System.out.println("implementation of method1");
}
Con..
public void method2()
{ System.out.println("method2");
}
public static void main(String args[])
{
MyInterface obj = new Demo();
obj.method2();
}
}
Output
implementation of method1