elibraryportal Logo

Interface in Java | Example

→ An interface is basically a kind of class, variable and methods. but difference is that interface define only Abstract method and Constant variable.

→ It is the responsibility of the class that implements an interface to define the code for implementation of these method.

→ In interface all the method are abstract method so we don't need to the abstract keyword before method.

→ In interface, all variable are define with "final" adn "static" keyword.

→ We can implements or inherit the interface one or more than within the class using "implements" keyword instead of "extends"

→ We want to inherit the one interface from other interface then we will use ""extends" keyword.

Syntax

Note
  • All variable are declared as constant inside an interface and methods are declared without body.
  • If we don't declare a variable as final static then it automatically takes it like a final static variable.
  • All method and variable inside interface has public access specifire.

Relationship between classes and interfaces

Interface in Java

Interface Example

Output:-

Interface Example in Java
Next TopicPackage in Java