Interface in Java

An interface in the Java programming language is an abstract type that is used to specify a behavior that classes must implement. It is a blueprint of class. Unlike the class, it contains only public & abstract methods and public, static & final fields.

One or more classes can implement a defined interface in java.When a class implements a defined interface, it has to implement (write the code, execution logic) for all the methods defined by the interface. The class is free to define more methods if necessary.Interface Implementation

 

Interface Syntax:

public interface InterfaceName
{
   //Any number of final, static fields
   //Any number of abstract method declarations\
}

Example:

//Implementation: by second user  

class Rectangle implements Drawable{  
public void draw(){System.out.println("drawing rectangle");}  
}  
class Circle implements Drawable{  
public void draw(){System.out.println("drawing circle");}  
}  

//Using interface: by third user  

class TestInterface1{  
public static void main(String args[]){  
Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()  
d.draw();  
    }
}

Difference between Interface and Class:

Interface Class
Have only abstract methods Cannot have abstract method
Multiple Inheritance possible Multiple Inheritance not possible
Can not be instantiated Can be instantiated
Do not have constructors Have constructor

Some facts about Interface:

  • Interface is used to achieve loose coupling
  • Multiple inheritance can be achieved by using interface
  • Interface provides full abstraction
  • Interface can extend multiple Interfaces
  • An interface does not contain any constructors, that is why Interface cannot be instantiated

InterfaceClass

package interface_package.constructor;

public interface InterfaceClass {
	int i=10;
	void inter();
}

MainClass

package interface_package.constructor;

public class MainClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		InterfaceClass interfaceClass=new InterfaceClass() // it will gave an error "Cannot instantiate he type 
                                                                   // InterfaceClass" because it is a Interface
	}
}
  • JVM itself writes the public static final before the data members and public abstract before methods in Interface if programmer does not write it

For Example:

public interface InterfaceClass {
	int i=10;
	void inter();
}

After Compilation
public interface InterfaceClass {
	public static final int i=10;
	public abstract void inter();
}

Interfaces are similar to abstract classes, but the differences between them are as follows:

  • All methods in an interface are abstract, which means all methods must be empty while in abstract classes no code is implemented
  • In an abstract class, the methods can have code/implementation within it but in abstract class, at least one method must be abstract
  • All properties (data fields) in an interface are static final while properties in an abstract class need not be static final
  • Interfaces are implemented(implements keyword) on the other hand Abstract classes are extended(extends keyword)
  • Class can extend only one abstract class whereas a class can implement multiple interfaces (multiple inheritance)
  • Contractual obligation: When a class specifies that it implements an interface, it must define all methods of that interface.

Complete Example Of Implementation Of Interface:

interface Ia{
	int x = 10;
	void a();
}
interface Ib{
	void b();
}
interface Ic extends Ia,Ib{
	void c();
}
abstract class Cc implements Ic{
	public void a(){
		System.out.println("A");
	}
}
class DerivedC extends Cc{
	public void b(){
		System.out.println("B");
	}
	public void c(){
		System.out.println("C");
	}
	public void d(){
		System.out.println("D");
	}
}
class Test{
 	public static void main(String args[]){	
		Ia obj = new DerivedC();
		obj.a();
		System.out.println(obj instanceof DerivedC);
		Ib obj1 = new DerivedC();
		obj1.b();
		System.out.println(obj instanceof Cc);
		Ic obj2 = new DerivedC();
		obj2.a();
		obj2.b();
		obj2.c();
		System.out.println(obj2 instanceof Ia);
		System.out.println(obj2 instanceof Ib);
		System.out.println(obj2 instanceof Ic);
		System.out.println(obj2 instanceof Cc);
		System.out.println(obj2 instanceof DerivedC);
	}
}

Github

For more Complete Examples on this topic visit codedbug Git Repository