Thursday, September 18, 2014

Interface in JAVA

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. 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.

Property of Interface:

1)Methods in an interface are implicitly public.
2)An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.
3)Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.

Example of interface in Java: 

Interface

interface DemoInterface extends Serializable{
int mobileNo();
}
 Java Class



class InterfaceImplementation implements DemoInterface {
private int mobileNo;
@Override
public int mobileNo() {
return mobileNo;
}
}
Interface is key to write flexible and maintainable code.

No comments:

Post a Comment