Thursday, 8 March 2012

Interfaces in Java

Interfaces:
•Interface is a set of methods without any bodies.
•An Interface is the point of communication between objects.
•An Interface is used to completely abstract/separate the behavior from a class.
•In Java, interface is similar to a class. Both are used to create new “types”.
•Interface contains only constants and method signatures.
•Interfaces, like abstract classes cannot be instantiated.
•Interfaces does not contain concrete methods.
•Through interfaces we obtain dynamic polymorphism, hence the name single interface multiple methods.
•Interfaces can only be “implemented” by other classes or “extended” by other interfaces.
•Interfaces are not part of the class hierarchies.
•Interfaces support multiple inheritance which is not supported by classes.
•A class can only extend a single class, but can implement one or more interfaces.


Defining an Interface:


Interface Body:
•Interface body consists of constant variables and method signatures.
•The methods declared in a Interface does not have any body.
•Methods end with a semicolon and does not contain any braces which are used to define the body of the method.
•Interface does not provide any implementation for the methods.
•All the methods in an Interface by default are “public” and “abstract”.
•All the constant variables declared in the interface by default are “public”, “static” and “final”.

Example of declaring an Interface:

Implementing an Interface:


•A class which implements an interface must implement all the methods of the interface.
•Since Java supports multiple inheritance through interfaces, if a class implements more than one interface, then the class must implement all the methods in all the interfaces.
•The methods implemented in the class must be declared as “public”.
•The method signature must also match with the signature of the method in the interface.




Partial Implementation of Interfaces:
•If the class implementing an interface does not implement all the methods in the interface, then it must be declared as an “abstract class”.
•A class which extends this abstract class must provide the implementations for all the methods in the interface.

Extending Interfaces:
•After declaring an interface and implementing the interface, in future at some point of time if we decide to add one or more new methods to the existing interface, then the classes which implements the interface will break.
•To prevent such breaking the existing interface can be extended by a new interface in which the additional methods are declared.
•Now, the class can simply implement the new interface and provide implementations for the new methods.

Applying Interfaces:
•Since interfaces cannot be instantiated, we will use the interfaces in our programs by using the concept, “Assigning an object of a one type to a variable/reference of a another type”.
•Here the one type will be the interface and another type will be the class which implements the interface.
•So, we can assign the object of a class which implements the interface to a variable/reference of the type, interface.

From our previous examples, we can write:
IShape s = new Rectangle();

Difference Between Interface and Class:
Differences between interface and a class

3 comments: