x

Java – Interface

PREV     NEXT

Java Interface:


Interface is similar to that of a class except that it can contain only constants and abstract methods.

Example:

  • Since the methods in interface are implicitly abstract, the keyword ‘abstract’ need not have to be prefixed.
  • Interface cannot be instantiated. It should only be implemented and used by the class implementing it.
  • A class can implement an interface using ‘implements’ keyword.

Syntax:

class <class_name> implements  <interface_name>

{

// Implementation

}

  • A class can implement more than one interface.

Example:


  • All the fields of an interface are implicitly public,static,final and all the methods are implicitly public, abstract.
  • A class which implements an interface has to implement all the methods declared in the interface otherwise it has to be declared as abstract.
  • An abstract class can implement an interface but it is not necessary to implement all the methods of the interface.
  •  An interface can inherit another interface by using ‘extends’ keyword.
  • When a class implements an interface that inherits another interface, then the class has to implement the methods of all the interfaces associated with the interface inheritance.

Example:

Example program using interface:

PREV     NEXT



Like it? Please Spread the word!