INTERFACES IN JAVA:
Interface in java is a similer to classes ,packages and sub packages.in the interface by default abstract methods and non abstrsct methods(methods body).
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of interface. Interfaces are used to declare method and variables without the implementation of the methods behavior. The class makes use of the interface by implementing the methods of the interface. The features of an interface.
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of interface. Interfaces are used to declare method and variables without the implementation of the methods behavior. The class makes use of the interface by implementing the methods of the interface. The features of an interface.
- Interface can't be instantiated
- An interface does not contain any constructors
- All the methods in an interface are abstract
Relationship between class and interface
An interface is similar to a class, but there are important differences
- Methods are declare in an interface. Implementation of the method should not be provide by an interface.
- The methods in interface are always public.
- The variables in an interface should be constant. The constant variables are inherited by the class which implements this interface
Interface interface_name
{
Interface body
}
note that the above structure, the methods are always public and there is no implementation of the method. Every variable given in interface is final, public and static. All methods in interface are public and abstract. The class which implements the behavior of the interface should be following syntax.
Class class_name implements interface_name
{
Public return-type methodname (parameter -list)
{
}
}
Example
interface shape
{
void show();
}
class Sample implements shape
{
public void show()
{
System.out.println("hello java");
}
public static void main(String args[])
{
Sample ob=new Sample();
ob.show();
}
}
o/p: hello java.