Thursday, 16 October 2014

Interface in Java

An interface can be treated as a class which contains only abstract methods (methods without body) and constants.  Interface is defined using the Java keyword interface. A class implements an interface by providing code for each method declared by the interface. By this mechanism an implementation rule or protocol is imposed on all classes they are implementing a particular interface or interfaces.

Here, as an example let us consider an abstraction called shape. We can identify some functions or methods of all shapes such as to calculate area, to calculate perimeter, etc. However, when you try to define a class Shape you cannot implement the method body because how to calculate area and perimeter in the context of Shape is undefined. But if you define a class called Rectangle or Circle, it is possible to implement the logic to calculate area and perimeter.

So we can say all shapes like Rectangle, Circle, Triangle, etc. have the same interface of a Shape. This idea can be implemented in Java by creating a Shape interface with the declaration of area () and perimeter () functions and Rectangle, Circle, etc. classes which will implement the Shape interface and by providing definition for the functions.

Interfaces provides a means to  achieve polymorphism also by assigning object of classes to the reference of interfaces implemented by the class and use the functionality of the class through the interface reference.

Following is a simple example of interface and its implementation

Source Code

Shape (interface)  –

public interface Shape {       
    double area();
    double perimeter();            

Rectangle (class)  –

public class Rectangle implements Shape {       
    protected double length;
    protected double width;
    public double getLength() {
        return this.length;
    public void  setLength(double length) {
        this.length = length;
    public double getWidth() {
        return this.width;
    public void  setWidth(double width) {
        this.width = width;
    public double area() {
        return this.length * this.width;
    public double perimeter(){
        return 2 * (this.length + this.width);

Circle (class) –

public class Circle implements Shape {       
    protected double raduis;
    public double getRadius() {
        return this.raduis;
    public void  setRadius(double raduis) {
        this.raduis = raduis;
    public double area() {
        return 3.14 * (this.raduis * this.raduis) ;
    public double perimeter(){
        return 2 * 3.14 * this.raduis;

ShapeTest (class) –

public class ShapeTest {       
   public static void main(String a[]) {
       Shape s;
       Rectangle r = new Rectangle();
       System.out.println("Area of Rectangle:" + r.area());
       System.out.println("Perimeter of Rectangle:" + r.perimeter());
       Circle c = new Circle();
       System.out.println("Area of Circle:" + c.area());
       System.out.println("Perimeter  of Circle:" + c.perimeter());
       s = r;      
       System.out.println("Area of Rectangle (Accessed through Shape):" + s.area());
       System.out.println("Perimeter of Rectangle (Accessed through Shape):" + s.perimeter());
       s = c;      
       System.out.println("Area of Circle (Accessed through Shape):" + s.area());
       System.out.println("Perimeter of Circle (Accessed through Shape):" + s.perimeter());

D:\java\interface>java ShapeTest
Area of Rectangle:10.0
Perimeter of Rectangle:14.0
Area of Circle:78.5
Perimeter  of Circle:31.400000000000002
Area of Rectangle (Accessed through Shape):10.0
Perimeter of Rectangle (Accessed through Shape):14.0
Area of Circle (Accessed through Shape):78.5
Perimeter of Circle (Accessed through Shape):31.400000000000002

How to test the program on your computer? 

1. Create file and copy the source code in the section 1.
2. Create and copy the source code in the section 2.
3. Create and copy the source code in the section 3.
4. Create and copy the source code in the section 4..
5. Compile the  interface and classes.
6.  Run the program by using the command java ShapeTest 

No comments:

Post a Comment