Java Interface Tutorial

What is Java Interface?

An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

Declaring Interface:

The interface keyword is used to declare an interface. Here is a simple example to declare an interface:

Syntax:

Rules for Declaring Interface:

  • All interface methods are public and abstract.

  • Interfaces can declare only Constant. Instance variables are not allowed. It means all variables inside Interface must be public, static, final. Variables inside interface are implicitly public static final.

  • Interface methods can’t be static.

  • Interface can extend one or more other interface. Note: Interface can only extend another interface.

Example:

Declaring Interface:

 

Implementing Interface:

Implement keyword can be used for implement interface.

Manikandan is Founder and author of codenread. Tech Blogger by Passion | Computer Science Engineer by Qualification