This site javatpoint is very useful for programming languages like JAVA,PYTHON,C,C++, Data Structures etc.Java learning Concept also provides technical materials , which are related to latest technologies. We provides very easy lectures to our viewers from beginning to advance level.We focus logical content instead of theoretical content.

Friday, 27 October 2017

Interface in java

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.

  • 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 
The behavior of the method is define in the class which implements an interface. Different classes may provide different implementation codes for the method declare in the interface. The methods return type should be public and the variables declaration should be constant. The general structure of the interface should be follow. 

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.



Adbox