Monday, November 16, 2015

interface in java with example

This is one of the important topic in java programming language which is regularly used in their software development projects. I am writing this article about interface concept with real time examples and their advantages.Then why late let's start.

An interface contains only abstract methods which are all incomplete methods. If you do not have method body you can not create object to an interface.Using interface keyword, you can specify what a class must do, but not how it does it. Interfaces are looks similar to classes,but they lack of instance variables and their methods are declared without the body.

From the client point of view interface defines the set of services what he is getting.From the service provider point of view interface defines the set of services what he is offering.Thus, interface acts as contract between client and service provider.

Defining an Interface:

Syntax:

access 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;
}

Example:

interface sample
{
public void m1();
public void m2();

The allowed modifiers for interface are: 

public
abstraction
strictfp
<default> 

When no access specifier included then default is result.

Interface variable:

An interface can contain variables also every interface variable is by default public static and final whether we r declaring or not.

The following declarations are equal inside interface:
 

int i = 10;
public int
i = 10;
public static int
i = 10;
public static final int
i = 10;

In interface variable we are not allowed the fallowing modifiers:

private, protected, volatile, transient

Example:

interface one
{
int i=20;
}
class Two implements one
{
public static void main(String args[])
{
i=30;
System.out.println(i);//compile time error should not assign final variable
}

NOTE: if you place int i=30 instead of i=30 then no error will rise because it is local variable to the main method.

Interface Method:

Every Interface method is by default public and abstract whether we r declaring or not.

The following declarations are equal inside interface.
 

void m1();
public void m1();
public abstract void m1();
 

The fallowing are not allowed in interface method:
 

modifiers.
private
protected
static
final
native
strictfp
synchronized


Example:  public abstract void display();

Real Time Scenario:

interface is not a class but set of requirements for classes.That means an interface is a specification. while developing software projects client gives instructions of their requirements in the form of interfaces, service provider can create classes where we can implement all the methods of the interface.These classes are called implemented classes.

                        I hope you have a credit card,you have to pay by using your credit card in dollars in a shop. If you another shop where they expect you have to pay in rupees. Here,the credit card is an interface which performs multiple tasks. In fact, the credit card is plastic card which does not hold money physically. It contains just your name,bank name and card number. Then how shopkeeper are able to with draw the money from credit card? so take implemented class as bank account which holds money from where it is transferred to the shop keepers.This bank account can be perform actual task.



The main advantages of Interfaces are:

  1. Security:The Third Party  not allowed to know internal implementation details.
  2. Enhancement: Without effecting end user or client we can perform any modification in the internal implementation 
  3. Improves Maintainability:  We can achieve communication between  two different systems( A java application can communicate with dotnet through interfaces)
Read also : Abstract in java


            



No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...