Java Interface doesn’t have to be hard. Read This!

(Last Updated On: July 16, 2018)

A nice note on Java interface in which interface variables, methods has been clearly explained with declaration and examples.  3 complete interface examples and interface FAQs has been included with answers.

Here is the topics on interface in java that will be covered in notes

  • What is interface in java and uses
  • Naming Convention of interface in Java
  • Java interface variables
  • Java interface methods
  • Simple Example of interface in java
  • Interface extends another interfaces example
  • Example of a class implementing 3 interfaces
  • Java Interface FAQ’s

What is interface in java?

An interface in java is a collection of abstract methods and or static and final variables. All the methods in an interface must be unimplemented and abstract to force the sub classes to implement it. So, the class who implements the interface, it must implement by all methods.

To declare an interface, we use interface keyword. Syntax of declaring an interface is given below.

 

 

In below figure, an interface A has a variable PI and a method f(). Class B is implementing and overriding  the f() method of interface A.
java interface

Why to use interfaces in Java?

We use Interfaces in java to achieve multiple inheritance in java programs and loose coupling in java or say abstraction as well.

 

Naming Convention of interface in Java

The name of an interface in java should be in camel case with starting letter in upper case. That’s it. For example, Set, Map and SortedSet (CamelCase) etc.

Mainly, in java application, we use naming convention for code readability purpose. Some people try to write interface name by prefixing with capital letter “I” e.g. IShape etc. But it violates the readability. In fact, name the interface what it is. For example, Shape interface.

If you have a look on java libraries source code, you should find the naming convention in camel case with first letter in upper case.

  • The name of an interface in java should be in camel case and with starting letter in upper case
  • Interface names cannot start with a number.

.

Java Interface variables

In an interface, variables are static and final by default. And can have only public access modifier. For example, here is a string variable declared in the interface.

syntax to declare variables within an interface.

 

 

Compiler by default understand it as

 

 

Java Interface methods

Interface in java contains only declaration of methods. By default, All the methods are public and abstract and they cannot have definition or implementation.

All methods of an interface must be overridden and implemented by the classes who implement the interface. In fact, abstract keyword forces the sub classes to implement them. If you miss any method implementation in subclass, the compiler will throw an error.

Interface method declaration in java

 

 

 

Simple Example of Interface in Java

In below java interface example, class Test will implement the method declared in interface A. Note that the class uses implements keyword in java to implement/inherit an interface.

 

 

 

Interface Extends another Interfaces example

in java, an interface can inherit/extends another interface. For example below, interface B is extending the interface A. So, the class who implement interface B, all methods from interface B and interface will be available to the class to implement it.

 

 

In fact, an interface can extend multiple interfaces like below.

 

 

Java Code – Interface extends another interface

In the java interface example, 2 interfaces has been created i.e. A and B. Interface B extends interface A. The class Implementer will implement only interface B. The methods of interface A will be also available to the class.

 

 

Output:
Interface A:fa()
Interface B:fb()

 

Example – A class implementing 3 interfaces

In below program, the class Implementer has implemented all 3 different interfaces A,B,C . In main program the object of the class has been created and invoked all methods.

Also, this example depicts that we can create a reference of an interface and assign object the class and invoke the method. But, in this case, all methods available in the interface for which you are creating a reference will be visible.

 

 

 

Java Interface FAQ’s

Q)Can interfaces be instantiated?

Answer: No interfaces cannot be instantiated like classes.

Q) Can an interface extend a class?

Answer: No, interfaces cannot extend classes.

Q)Can it also have a constructor?
Answer:  Since, Interfaces don’t have methods implemented and are abstract, so, there is no meaning of having constructor in it.

Q) Can an interface be final?

No, an interface in java cannot be final. An interface is a pure abstract class. Hence, all methods in an interface are abstract that must be implemented by child classes. The purpose of final is to stop inheritance. So, it is contradictory that by using final interface, we want to stop it to be extended and saying child classes to inherit it. You can read more about final keyword in java where examples has been demonstrated.

Q) Can an interface implement another interface?

No, interfaces in java extends another interface not implement them.