You are on page 1of 6

Interface in Java

Def:
An Interface is basically a kind of class. Like classes, an interface contains final fields
(constants) and abstract methods.
 There is responsibility of the class that implements an interface to define the code
for implementation of these methods

Why we use Interface?


 By using Interface, we can achieve multiple inheritance in java.

Syntax of an interface:
The interface keyword is used to declare an interface.

interface InterfaceName
{
datatype variablename=value;
//Any number of final, static fields
returntype methodname(list of parameters or no parameters)
//Any number of abstract method declarations
}

Explanations

In the above syntax Interface is a keyword interface name can be user defined name the default

signature of variable is public static final and for method is public abstract. JVM will be added

implicitly public static final before data members and public abstract before method.

Example

public static final datatype variable name=value; ----> for data member

public abstract returntype methodname(parameters)---> for method

Properties of Interface
 Each method in an interface is also implicitly abstract, so the abstract keyword is not
needed.
 Methods in an interface are implicitly public.
 All the data members of interface are implicitly public static final.

Here is an example of an interface definition contains two variables and one methods.
Example 1:

interface Item
{
static final int code = 1001;
static final String name="Fan";
void display();
}

Example 2:

interface Area
{
static final float PI = 3.142f;
float compute(float x, float y);
void show();
}

Extending Interfaces in Java


Like classes, Interface can also extend another interface. That means an interface can
be subinterfaces from other interfaces. The new subinterface will inherit all members of the
superinterface similar to subclasses. It can be done by using a keyword "extends". It has the
following general form:

interface interfaceName2 extends interfaceName1


{
// body of interfaceName2.
}

Example1: Extending an interface:


interface ItemConstant
{
int code=1001;
String name="Fan"
}
interface Item extends ItemConstant
{
void dispay();
}
Example 2: extends two interfaces:

We can combine several interfaces together into a single interface. Following declarations are
valid.
interface ItemConstant
{
int code=1001;
String name="Fan"
}
interface ItemMethods
{
void dispay();
}
inteface extends ItemMethods, ItemConstant
{
----------------------
----------------------
}

 Sub_interface cannot define the methods of super_interface

 After all sub_interfaces still interfaces.

 Interfaces can extends interface

 But interface cannot extends interfaces.

Implementing Interfaces:
An interface is used as "superclass" whose properties are inherited by a class. A class
can implement one or more than one interface by using a keyword implements followed by a
list of interfaces separated by commas.
When a class implements an interface, it must provide an implementation of all
methods declared in interface and all its superinterfaces. Otherwise, the class must be
declared as abstract.

The syntax of interface implementation using a class is shown below.


class className implements interfaceName
{
// method implementations;
// member declaration of class;
}
Here the class className “implements” the interface interfaceName. A more general form
of interface implementation is given below.

class className extends superClass implements interface1, interface2, …..


{
// body of className.
}

This general form shows that a class can extend another class while implementing interfaces.

Various forms of interface implementation


Java program for to Calculate Area of Rectangle and Area of Circle
Using Interface

interface Area
{
double pi = 3.14;
double calcArea(double x, double y);
}

class Rect implements Area


{
public double calcArea(double x, double y)
{
return(x*y);
}
}

class circle implements Area


{
public double calcArea(double x, double y)
{
return(pi*x*x);
}
}

class InterfaceTest
{
public static void main(String arg[])
{
Rect r = new Rect();
circle c = new circle();
System.out.println("\nArea of Rectangle is : " +r.calcArea(10,20));
System.out.println("\nArea of Circle is : " +c.calcArea(15,15)); }
}

OUTPUT:
C:\Users\staff1\Desktop>javac InterfaceTest.java

C:\Users\staff1\Desktop>java InterfaceTest

Area of Rectangle is : 200.0

Area of Circle is : 706.5


/*
Lab Program 11: Write a program to demonstrate use of implementing
interfaces.
Save as: Multiple.java
*/
import java.io.*;
interface Circle
{
float pi=3.142f;
float compute(float r);
}
class Rect
{
float compute(float l,float b)
{
return (l*b);
}
}
class Area extends Rect implements Circle
{
public float compute(float r)
{
return pi*r*r;
}
void display()
{
System.out.println("Area of Rectangle :"+compute(10f,20f));
System.out.println("Area of Circle :"+compute(5f));
}
}
class Multiple
{
public static void main(String args[])
{
Area a=new Area();
a.display();
}
}
--------------------------------------------------------------
OUTPUT:
C:\Users\staff1\Desktop>javac Multiple.java
C:\Users\staff1\Desktop>java Multiple
Area of Rectangle :200.0
Area of Circle :78.55

You might also like