Interface

Interfaces, like the abstract classes and methods, provide templates of behavior that other classes are expected to implement, but they are much more powerful.

An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. An interface is not a class.

However writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements. Java provides a keyword interface for declaring an interface, as class for declaring a class.

All the methods declared inside an interface must be public and abstract.

As we used extends keyword to inherit a class, implements keyword is used to implement an interface.

A class that implements an interface must implement all of the methods described in the interface. A class can implement more than on interface at a time therefore providing the facility of multiple inheritance.

An interface may extends another interface. However an interface cannot implements another interface.

Similar to abstract class and interface cannot be instantiated. An interface cannot contain constructor, instance variables and methods (contains only abstract methods and static final variables).

By default all the methods declared inside an interface are public and abstract. We do not need to use public and abstract modifier.

Interfaces are used to encode similarities which the classes of various types share, but do not necessarily constitute a class relationship.

For an example; Human, Animal and Bird all eat. However it does not make sense to declare Human, Animal and Bird class as the subclass of the superclass FoodHabit. They all belong to three different classes but all of them can implement a FoodHabit interface to define their own eat method.

This is the general form of an interface:


interface name 
{
	return-type method-name1(parameter-list); 
	return-type method-name2(parameter-list); 
	type final-varname1 = value;
	type final-varname2 = value; 
	// ... 
	return-type method-nameN(parameter-list); 
	type final-varnameN = value;
	}

Here, access is either public or not used. When no any access specifier is included, then default access results, and the interface is only available to other members of the package in which it is declared.

When it declared as public then, the interface can be used by any other code. name is the name of the interface, and can be any valid identifier.

Note- that the methods which are declared have no bodies, after the parameter list They are end with a semicolon. That are, essentially, abstract methods; there can be no default implementation of any method specified within an interface.

Each class that are includes an interface must implement all of the methods. Variables can be declared inside of interface declarations. That are implicitly final and static, meaning they cannot be changed by the implementing class. They must be initialized with a constant value.

All methods and variables are implicitly public if the interface, itself, is declared as public. This is an example of an interface definition. It is declares a simple interface which contains one method called callbackexample( ) that takes a single integer parameter.


interface Callbackexample 
{ 
	void callback(int para); 
}

Declaring an interface


interface InterfaceName  
{ 
 //abstract methods or static final variables 
} 

implementing an interface


class classname implements interface1,interface2,… 
{ 
 //overrides methods of interface1 , interface2 and so on…. 
} 

Let’s take the same example of Shapes. Declare Shape as an interface with abstract method area and peri and implement it across various shapes.


interface  Shape 
{
	static final float pi=3.14;  
	void area();  
	void peri(); 
} 
class Circle implements Shape 
{  
	//It is mandatory to override both the methods of Shape interface  public float radius;    
	@override  public  void area()  
	{  
	 System.out.println("Area of the Circle is "+(pi*radius*radius));  
	}  
	@override  public void peri()  
	{   
		System.out.println("Perimeter of the Circle is "+(2*pi*radius)); 
	} 
}
class Rectangle implements Shape 
{  
	//It is mandatory to override both the methods of Shape interface  public float length,breadth;    
	@override  public  void area()  
	{  
		System.out.println("Area of the Rectangle is "+(length*breadth));  
	}  
	@override  public void peri()  
	{   
		System.out.println("Perimeter of the Circle is "+(2*(length+breadth))); 
	}
 } 

Now create a ShapeTest class with the main method to test the functionality.


 public class ShapeTest 
{  
	public static void main(String ar[])  
	{  
		//assigning a Circle object to myshape reference of Shape type.   
		Shape myshape=new Circle();   
		myshape.radius=5.25;   
		myshape.area();  
	}
}

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group