Interfaces in Java

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Java interface is a just like a class, except you can only declare methods and variables in the interface but you cannot actually implement the methods, an interface is declared using the Java keyword interface. Just like with classes, an interface can be declared public or package scope (no access modifier). And multiple inheritance problem is solved with a powerful concept interfaces.

Interface Example:

public interface TestInterface {

    public String hello = "Hello";

    public void sayHello();
}
 

Here are some rules that needs to be followed by Interface.

  • All Interface methods are implicitly public and abstract. Even if you write these keyword it will not create problem.
  • Interfaces can declare only Constant. Instance variables are not allowed. Variables inside interface are implicitly public static final.
  • Interface methods cannot be static.
  • Interface methods can not be final.
  • Interface can extend one or more other interface.

Suppose we want to create a drawing of multiple shapes, so we can create an interface Shape and define all the methods that different types of Shape objects will implement. For this purpose, we can keep only two methods draw() to draw the shape and getArea() that will return the area of the shape.

Interface Example

Based on above requirements, our Shape interface will look like below.

package com.javatutsworld;

public interface Shape {

	//implicitly public, static and final
	public String LABLE="Shape";
	
	//interface methods are implicitly abstract and public
	void draw();
	
	double getArea();
}

First interface implementation Circle Class for Shape interface.

package com.javatutsworld;

public class Circle implements Shape {

	private double radius;

	public Circle(double r){
		this.radius = r;
	}
	
	@Override
	public void draw() {
		System.out.println("Drawing Circle");
	}
	
	@Override
	public double getArea(){
		return Math.PI*this.radius*this.radius;
	}

	public double getRadius(){
		return this.radius;
	}
}

Second interface implementation Rectangle Class for Shape interface.

package com.javatutsworld;

public class Rectangle implements Shape {

	private double width;
	private double height;
	
	public Rectangle(double w, double h){
		this.width=w;
		this.height=h;
	}
	@Override
	public void draw() {
		System.out.println("Drawing Rectangle");
	}

	@Override
	public double getArea() {
		return this.height*this.width;
	}

}

below is a Test Class which showing how to code work.

package com.javatutsworld;

public class Test {

 public static void main(String[] args) {
		
	//programming for interfaces not implementation
	Shape shape = new Circle(5);
		
	shape.draw();
	System.out.println("Area of Circle="+shape.getArea());
		
	//switching from one implementation to another easily
	shape=new Rectangle(15,15);
	shape.draw();
	System.out.println("Area of Rectangle="+shape.getArea());
	}

}

Output:

Drawing Circle
Area of Circle=78.571428571428
Drawing Rectangle
Area of Rectangle=225.0
      Advertisements